Link Search Menu Expand Document

Git Cheat Sheet


Create New Branch

Create a new git branch and switch to it

git checkout -b <branch name>

and create it on origin too

git push --set-upstream origin/<branch name> <branch name>

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 --track <local name> origin/<name>

Remove Branch

Remove git branch locally with

git branch -d 

and remove on origin too

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

Alternatively remove on origin and then prune locally.

Rename Local Branch

git branch -m <new name>

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 <files>
git rebase --continue

Move commits onto new branch

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

Squash Commits

git rebase -i <commit after which to rebase>


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 <files>  # 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 [email protected]{0}        # Show changes file in last stash
git stash show -p [email protected]{0}     # Show patch for last stash

git stash apply [email protected]{0}       # Get last stash active again
git stash drop [email protected]{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 <files>
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 -- 


git disect start <recent commit> <older commit>

git disect good
git disect bad

git disect reset


git fetch --all --tags --prune

git checkout tag/<tag> -b <branch>
git clone <repo> --branch=<tag>

git tag                    # List all tags
git tag <tag>              # Create tag
git tag -a <tag> -m <msg>  # Create annotated tag
git push --tags            # Push tags to remote

git tag -d <tag>         # Deletes tag in your local repo
git push origin :<tag>   # Deletes tag remote


Enable git password Caching

To keep passwords for 1h run

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

Allow insecure certificates

One time only

git clone <url> --config http.sslVerify=false

Permanently for a give URL

git config https.<remote url>/.sslVerify false

Configuring specific proxy

Disable proxy for a given URL

git config https.<remote url>/.proxy ""


Update submodules

git submodule update --init --recursive

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='\[email protected]\h:\w\$ "

you could extend it to

export PS1='\[email protected]\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

Merge two repos

One simple way to merge two repos is to add one repo (repo1) into a subdirectory of another repository (repo2)

cd repo2
git remote add repo1 <path to repo1>
git fetch repo1
git merge -s ours --no-commit repo1/master
# Ignore the merge error!

git read-tree --prefix=<subdir> -u repo1/master
git commit

That’s it. Check “git log” to see if changes of repo1 appear.

Comfort merging with opening PRs in a browser

This can be done via git command aliases invoking xdg-open. An example solution

  curbranch = rev-parse --symbolic-full-name --abbrev-ref HEAD
  bpush = !sh -c 'git push origin $(git curbranch) 2>&1 |grep -o "https://.*" |xargs xdg-open'
  bp = !sh -c 'echo "Pull" && git pull origin master && echo "Push" && git bpush'

with now “git bp” pulling, pushing and opening the PR for further review work.

Checkout at a specific time

git checkout '[email protected]{2019-01-01 01:00:00}'

<?slideshare,cWwH3B15RuuGW6,Git Tips and Tricks?>

cherrypick changes in a specific commit from branch ‘A’ to branch ‘B’

git checkout B
git cherry-pick <commit hash from branch A>

know what changed on a specific commit

git show <commit hash>

Resync git repo

git fetch origin && git reset --hard origin/master && git clean -f -d

Resync forked repo

git remote add upstream
git fetch upstream
git checkout master
git merge upstream/master

Delete remote branch

git push origin --delete <branch>

Ignore invalid certificates

git -c http.sslVerify=false <command>