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:

git commit -m "<your_description>"
git push


– View Commit History

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

Branch Commands

– List all remote and local branches

git branch -a


– Create a new branch

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

# Push the newly created local branch to remote
git push -u origin <new_branch_local_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/*


– Delete a Local Branch

git branch -d <local_branch_name>


– 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.