Getting Started with Git commands

List of git commands to work through the commit tree

·

5 min read

Thanks to https://learngitbranching.js.org/ for their amazing work.

Git is a version control system but to reduce the struggle of working through the commit tree the following set of commands will always come handy where you not only can understand what a command does but also know certain set of commands that you will use very often.

Create a new branch

git branch branch_name

Go to a branch

git checkout branch_name

Commit changes to the tree

git commit

Rebase current branch to new branch

git commit new_branch_name

 Head is the symbolic name for currently checked out commit. Head always points to recent commit.

Detaching head: It means attaching head to a commit instead of a branch.

In real world git commit have specific hashes. Git has relative commits to make things easier.

Moving one parent above at a time

git checkout branch_name^

Move n number of parents up in the tree

git checkout branch_name~number

Moves(by force) the current branch to n parents behind Head. Directly reassign a branch to a commit with -f option.

git branch -f current_branch Head~n

-move current_branch to 3 parents behind Head forcefully -Head~n is the target branch and current_branch will move to Head~n after command is executed

Git reset reverses the change by moving a branch reference backward in time to an older commit. Git commit moves the branch backward as if the commit never happened

git reset HEAD~1

  • Will bring the head to immediate parent and will remove the commit on which it was initially present. This only works on local commits and not on remote commits.

When we work with remote branches, rewriting history or git reset won't work. We use git revert.

 git revert HEAD

This command will revert changes of current commit(HEAD in this example) and will make a new commit.

Copy a series of commits below your current location(HEAD)

git cherry-pick <Commit1> <Commit2> <...>

The new main will be the last commit. ex: git cherry-pick C2 C4

-After executing the command the new main will be at C4

cherry-pick is simple to use when we know the hashes of the branches. When we don't know hashes of the branches this command is useful

git rebase -i HEAD~4

-First git will go to the 4th ancestor of HEAD.

-The branches that you have selected will be committed below the 4th ancestor.

Make a slight modification in a commit

git commit --amend

Add the branch_name below the current branch you are at

git cherry-pick branch_name

Consider you have some major releases or milestones in the project. Git tags help to mark certain commits as anchor points in the commit tree

git tag tag_name branch_name

  • Here branch_name will get a tag of tag_name. You cannot commit directly to the tag_name

git describe <ref>

is anything git can resolve into commit. If you don't specify a ref, git will use where you are checked out right now.

Output of git describe is of the following form :

tag_num_of_commits_ghash

tag- closest ancestor tag in history. num_of_commits - number of commits away the tag is hash- hash of the commit being described

Select among the parents while moving up the commit tree

git checkout branch_name^number

'number' will be the parent number.

When there are more than one parent ^number helps to decide which parent we are going to choose while move up from the current branch

Clone a repository on a different computer

git clone

Remote branches have a naming convention :

remote_name/branch_name

Git sets up the remote main as named 'origin'.

Remote branches are on local repository and not on remote repository. The commits and changes that we make will be made locally until we push our commits.

The branch appearing on our local repository is origin/main.

Remote branches are named as per following conventions:

remote_name/branch_name

Git sets up the remote repository as origin when we git clone.

Downloads all the commits from remote repository that are missing to our local repository.

Git fetch synchronizes the local repo with remote repo. It is a download step and it doesn't change local files or our main branch

git fetch

Fetch the remote repo and merge them at once

git pull

Git push uploads the changes to a specified remote and update the remote to incorporate the new commits.

git push

Following set of commands are useful when pull and merging together

  git fetch; - update local representation of remote
  git rebase o/main; - rebase our current work wrt to updated remote
  git push; - push our work to remote repo
  git fetch;
  git merge o/main; 
  git push;

git pull --rebase This will do a pull and a rebase. Git pull is a shorthand of fetch and a rebase. So we can do

  git pull --rebase;
  git push

We can also do

  git pull;
  git push;

git pull clone the remote repo on our local repo and merges it as well on our local repository. Then git push will send the updated the commits to the remote repo from the point where we committed.

When main is locked

Create another branch called feature and push that to remote. Also reset your main back to be in sync with the remote otherwise you have issues next time you do a pull and someone else's commit conflicts with yours.

  git branch feature_branch
  git checkout feature_branch
  git push 
  git branch -f main feature^

Using git rebase

git rebase target_branch branch_to_be_rebased

branch_to_be_rebased will be added below target_branch.

Set remote tracking:

git branch -u o/main foo This will set foo branch to track o/main If we are checking out foo, we can leave it like this

git branch -u o/main

git push <remote> <place>

ex: if command is git push origin main it means go to main, grab all the commits and send it remote named origin.

The command exclusively means something like this -> git push target_remote_repo branch_name.

git push origin <source>:<destination>

: shows the source and destination of branch name.