Common Git Command Usage

We introduce a number of commonly used git commands here. Knowing these commands should be enough for most of you to handle your day-to-day development routine.

A Quick Glance

git clone <url> <optional_local_dir_name> # clone to the specified folder
git clone -b <remote_branch_name> <url> # clone a specific branch
git fetch origin
git branch -a # list all remote and local branches
git checkout -b <desired_local_name> <origin/remote_branch_name> # checkout a remote branch
git checkout -b <new_branch_local_name> # create a new local branch
git push -u origin <new_branch_local_name> # push the newly created local branch to remote
git checkout -- <file_path> # discard any change of a file
git reset --hard # reset everything to current commit
git reset HEAD <file_path> # unstage a file
git rm <file_path> # stage "deleting" a file
git add * # stage all changed files
git commit -m "New commit message."
git push

Stage/Commit Commands

– Check staging status

git status


– View Changes

# show all changes
git diff

# show changes of a file
git diff <file_path>


– Stage Changes

To add (stage) a changed file:

git add <file_path>

Or add all changed files:

git add *

To add (stage) a removed file (to commit to “remove” the file):

git rm <file_path>


– Unstage Changes

Unstage files that have been staged:

git reset HEAD <file_path>


– Discard Changes

To discard any change of a file (so the file falls back to the intact version):

git checkout -- <file_path>


– Reset All Changes

To discard all changes and reset your local repo to current commit:

git reset --hard


– Commit Commands

Add message, commit and push to remote:

# to commit in a single line
git commit -m "<your_description>"

# to commit and write a longer message text
git commit -a

# to revise the current committed message
git commit --amend

# to push
git push


– View Commit History

# to view commit history of the entire repository
git log

# to view commit history of the current directory
git log .

This article suggested a way to beautifully display commit logs with using the following method:

# set an alias for the pretty log command
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"

# use the new command
git lg

Amend and Rebase Commands

– Amend the current (committed) commit

After you have done some additional changes, git add them and use the following command:

git commit --amend
which will add your changes into the current commit and allow you to modify the commit message.


– Unstage a file committed in the current commit

When you have changed a file and accidentally add/rm the file in the current (committed) commit, use the following commands to unstage the file and remove the change from the commit:

# unstage a file
git reset HEAD~1 <file_name>

# amend the current commit
git commit --amend

Note that this applies to deleting a file as well.


– Rebase (simple cases)

In most cases, you work on a dev branch and you will want to rebase with the up-to-date master. To the following:

git checkout master
git pull

git checkout <your_dev_branch>
git rebase master


– Rebase to an dependent branch with amended commits

In the case that you create a branch, say dev-2, based on another branch, say dev-1, and both are under development, then things are getting complex.
When you have a commit amended in dev-1 and you want to rebase your dev-2 to dev-1, you should do the following:

git checkout dev-2
git rebase -i dev-1

and in the interactive page mark the amended commit with drop.
This way, the old commit from dev-1 will be dropped and new ones from current dev-1 will be replayed.

Branch Commands

– List all remote and local branches

git branch -a


– Create a new branch

# Create a new local branch
git checkout -b <new_local_branch_name>

# Push the newly created local branch to remote
git push -u origin <new_local_branch_name>


– Rename an existing local branch

git branch -m <old_local_branch_name> <new_local_branch_name>


– Delete a Local Branch

git branch -d <local_branch_name>


– Check out a remote branch

git fetch origin
git checkout -b <desired_local_name> <origin/remote_branch_name>

Alternatively, you can specify the remote branch name whgen cloning the repo:

git clone --branch <remote_branch_name> <repo_url>

or with a short parameter label

git clone -b <remote_branch_name> <repo_url>


Can’t check out or don’t see your branches?

If you don’t see all the branches from git branch -a, it’s likely that you did the clone with a specific branch (e.g., git clone -b or git clone --depth=1). If there is no specific reason, then avoid using those branch-specific clone commands will resolve the issue. If you really don’t want to start again from scratch, then here are two ways to get your remote branch(es) on board:

  1. Fetch a specific branch (source):
    git fetch origin '<remote_branch_name>':'<desired_local_branch_name>'
  2. The fetch configuration is stored in the file .git/config. Find the parameter fetch under the section [remote "origin"] and revise it to:
    fetch = +refs/heads/*:refs/remotes/origin/*


– Check out a Branch from Upstream

If you have a forked repository and there is a new branch in the upstream your repository doesn’t follow, then the following commands are useful to get such a branch in your repository.
Some people may suggest rebasing your branch, but it didn’t work in my case (due to complex reasons).
In the end, the following commands work for me:

git remote add upstream <remote_url>
git fetch upstream
git checkout -b <desired_local_name> upstream/<upstream_branch_name> # check out the desired branch from upstream 
git push -u origin <local_branch_name> # push this new branch to my remote repo

# optional clean-up
git remote rm upstream

Config/Info Commands

– Read/Revise Remote URL

git remote -v # list all the remote names and URLs
git remote set-url <remote_name> <remote_url> # set/revise

Bonus: Compare Two Commits on GitHub

In the following link, either a branch name or a commit ID can be used for comparison.

# Example:


Was this post helpful?

Leave a Reply

Your email address will not be published.