Reading time ∼7 mins

Git Cheatsheet

Table of contents

A yet another Git cheatsheet


Setup a local directory as a Git repository

git init
git init <directory_path>

Retrieve a repository from remote through a URL

git clone <repository_url>
git clone -b <branch> <repository_url>


Remove --global flag to set the configuration only for a given Git project.

Set a name and email to associate with the history

git config --global user.name "<name>"
git config --global user.email "<email_address>"

Set automatic command line coloring to review easily

git config --global color.ui auto

Enable autocorrect (with a 1.5 second delay)

git config --global help.autocorrect 15

Set an alias for a command

git config --global alias.<new_alias> <git_function>

Use quotes if the alias contains multiple functions.

Unset a configuration

git config --unset-all user.name
git config --unset-all user.email

List the configurations

git config --global --list

Stage & Snapshot

Show modified files in the working directory, staged for the next commit

git status
git status -sb

-sb flag displays a styled status, instead of the plain one.

Add a file or directory to the next commit

git add <path>

Add all the modified files to the next commit

git add .

Stripspace a file

Strip trailing whitespaces, collapse newlines and add a newline at the end of a file with stripspacing.

git stripspace < README.md

Show files to be removed from working directory

# dry run
git clean -n

# clean the files
git clean -f

# clean the files and directories
git clean -f -d

Unstage a file while retaining the changes in working directory

git reset <file_path>


Revert a commit to undo all the changes made in it, then apply it to the current branch

git revert <SHA>[ <SHA> ...]

Revert n commits by providing as many SHAs.

Revert last N commits

git revert HEAD~N..HEAD

Revert a range of commits

git revert <SHA1>..<SHA4>


Commit the staged content

git commit -m "<message>"

Commit all the local changes in tracked files

git commit -a

Commit with no change

git commit -m "<message>" --allow-empty

Amend a previous commit

git commit --amend --no-edit

This is useful when the new changes are trivial enough not to warrant a new commit over the previous one.

Commit without triggering any precommit or commit-msg hooks

git commit --no-verify

Temporary Commits

Save modified and staged changes

git stash

Save a subset of modified and staged file(s)

git stash push -m <message> <file_path>[ <file_path1> ...]

Stash untracked files

git stash push --include-untracked

List stack-order of stashed file changes

git stash list

Write working from top of stash stack

Pop deletes the stash from the stack after it has been applied on the repository.

git stash pop
git stash pop stash@{<stash_id>}

Specify a <stash_id> to write a particular stash on the repository and subsequently delete it from the stack.

Write a particular file from stash

git checkout stash -- <file_path>
git checkout stash@{<stash_id>} -- <file_path>

Specify a <stash_id> to unstash a file from a particular stash and write it in the repository.

Write a particular file from stash on a new branch

git stash branch <branch_name>
git stash branch <branch_name> stash@{<stash_id>}

Apply the top of stash without deleting it

git stash apply
git stash apply stash@{<stash_id>}

Specify a <stash_id> to write a particular stash on the repository without deleting it from the stack.

Display a summary of stash diffs

git stash show
git stash show -p
git stash show stash@{<stash_id>}

Use -p to display the full diff. Specify a <stash_id> to display the diff summary of a particular stash.

Discard the changes from top of stash stack

git stash drop
git stash drop stash@{<stash_id>}

Specify a <stash_id> to drop a particular stash.

Discard all stashes in a repository

git stash clear

Ignore Patterns

System wide ignore pattern for all local repositories

git config --global core.excludesfile <file_path>

Ignore a file only on local repository

git update-index --assume-unchanged <file_path>

Vice-versa, to unignore a file, use --no-assume-unchanged flag in the above command.

Ignore files in a directory only on local repository

cd <directory_path>
git ls-files -z | xargs -0 git update-index --assume-unchanged

Save a file with desired patterns as .gitignore with either direct string matches or wildcards


Branch & Merge

List the branches

git branch

A * will appear next to the currently active branch.

Create a new branch at the current commit

git branch <branch>

Delete a local branch

git branch -d <branch>
git branch -D <branch>

-d is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch. Use -D, which is an alias for --delete --force, to delete the branch irrespective of its merged status.

Delete a remote branch

git push <remote> --delete <branch>

In most cases, <remote> is origin.

Switch to another branch and check it out into your working directory

git checkout -b <branch>
git checkout -

Switch to the previous branch by using -.

Create an orphan branch

git checkout --orphan <branch>
git rm -rf .

Merge the specified branch’s history into the current one

git merge <branch>

Find all branches that have been merged into the current branch

git branch --merged

Vice-versa, list the branches that have not been merged into the current branch using the flag --no-merged.

Inspect & Compare

Show any object in Git in human-readable format

git show <SHA>


Show the commit history for the currently active branch

git log

Show the commits on branchA that are not on branchB

git log branchB..branchA

Show the commits that changed file, even across renames

git log --follow <file_path>

Show all commit logs with indication of any paths that moved

git log --stat -M

Limit number of commits

git log -<limit>

Condense each commit to a single line

git log --oneline

Styled log to print logs in prettyprint

git log --all --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative


Diff of what is changed but not staged

git diff

Diff of what is staged but not yet committed

git diff --staged

Show the diff of what is in branchA that is not in branchB

git diff branchB...branchA

Track Path Changes

Delete the file from project and stage the removal for commit

git rm <file_path>

Remove all deleted files from the working tree (synonym of git add . but only for deleted files)

git rm $(git ls-files -d)

Change an existing file path and stage the move

git mv <existing_path> <new_path>

Search & Browse

Search all previous commit messages and find the most recent one matching the query

<query> is a case-sensitive search term

git show :/<query>

Press q to quit the query.

Find a list of lines matching a pattern from all files

git grep <keyword>
git grep -e <regex or glob>

Besides a keyword, you can also use regex or glob patterns. You can even chain multiple expressions through --and, --or and --not flags.

Web Server for Browsing Local Repositories

git instaweb

Share & Update

Add a git URL as an alias

git remote add <alias> <url>

Fetch down all the branches from that Git remote

git fetch <alias>
git fetch <alias> <branch>

Merge a remote branch into the current branch to bring it up to date

git merge <alias>/<branch>

Transmit local branch commits to the remote repository branch

git push <alias> <branch>

Fetch and merge any commits from the tracking remote branch

git pull

Rewrite History

Apply any commits of current branch ahead of specified one

git rebase <branch>

Show a log of changes to the local repository’s HEAD

git reflog

Clear staging area, rewrite working tree from specified commit

git reset --hard <commit>
git reset --hard <branch>@{<spec>}

<spec> could be an SHA or a fuzzy time like "4 hours ago"

Useful Aliases

aliases = config --get-regexp alias
amend = commit --amend --no-edit
branches = branch -a
post = commit -m
remotes = remote -v
saga = log --oneline
tags = tag -l


Other cheatsheets/references