Git Cheat Sheet


Create New Branch

Create a new git branch and switch to it

git checkout -b 

and create it on origin too

git push origin 

Tracking Remote Branches

To follow additional branches in your local repo follow these steps:

# Find out all existing remote branches
git branch -r

# And track one of them locally
git branch --set-upstream-to  

Remove Branch

Remove git branch locally with

git branch -d 

and remove on origin too

git branch -d -r origin/
git push origin :

Alternatively remove on origin and then prune locally.

Remove stale

When remote branches disappear clean them from your local repo with

git remote prune origin


To rebase on master

git rebase master 

Solve Merge Conflicts

When a rebase fails manually fix files and

git add 
git rebase --continue

Move commits onto new branch

git branch 
git reset --hard HEAD~1      # 1 to move 1 commit
git checkout 


Amending changes

git add 
git commit --amend

Apply patches

A detailed description can be found here. You should always run the following commands:

git apply --stat cool_feature.patch    # Check what the patch will do
git apply --check cool_feature.patch   # Check if the patch fails
git am --signoff < cool_feature.patch

Create patches

git format-patch -1     # Creates one patch file for the commit
git format-patch -2 HEAD           # Creates two patch files for last two commits on HEAD
git format-patch -3 HEAD --stdout  # Print last 3 commit changes on stdout

Solving Mistakes

Accidental "git add"

git reset HEAD []

Accidental commit of too many files

git reset --soft HEAD^

git status		# to list all added files
git reset 	# to remove incorrectly added files

# Commit afterwards

Stashing Changes

For a detailed explanation check here. This feature is useful to put debugging or experimental changes to the "background". Here are the commands:

git stash "Some test I made"    # Stash some changes away

git stash list                  # List stashes
git stash show stash@{0}        # Show changes file in last stash
git stash show -p stash@{0}     # Show patch for last stash

git stash apply stash@{0}       # Get last stash active again
git stash drop stash@{0}        # Delete last stash

git stash pop                   # Apply and remove last stash

git stash clear                 # Delete all stashes

# Stash just some files by adding all others first 
# and using --keep-index
git add 
git stash --keep-index

List Commits in One Line Each

git rev-list --all --pretty=oneline

Search in Commits

To search all commits for lines containing a certain change:

git log -p -S -- 
git log -p -G -- 


Update submodules

git submodule update --init --recursive

Enable git password Caching

To keep passwords for 1h run

git config --global credential.helper 'cache --timeout=3600'

Remove all repo files from a directory

If you ever need to remove all git related files from a local working repo and make it just a normal directory:

git clean -ffrx

List Branch in Bash Prompt PS1

There are several documentations online e.g. this one. In the end it boils down to running "git branch" on each prompt and to use an environment function __git_ps1() that is set up by "git branch" to print the branch if there is one. So if your current $PS1 is

export PS1='\u@\h:\w\$ "

you could extend it to

export PS1='\u@\h:\w$(git branch &>/dev/null; echo $(__git_ps1 "(%s)"))\$ '

to show the branch name in braces after the directory name and before the $ like this:

[email protected]:~/project/src(master)$ 

Push Dry Run

git push --dry-run --porcelain

git-write-tree: error building trees

git reset --mixed