Janik von Rotz

3 min read

Git Cheat Sheet

A simple git cheat sheet.


Change last commit message.
git commit --amend


List remotes.
git remote -v

List remote branches.
git branch -r

Add multiple push urls.

git remote set-url origin --push --add <a remote>
git remote set-url origin --push --add <another remote>

Add upstream.
git remote add upstream ssh@example.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

Update fork.

git fetch upstream
git merge upstream/main

Resolve history mismatch.

git fetch --all
git reset --hard origin/master


List tags.
git tag

Add tag.
git tag $TAG_NAME

Delete tag.
git tag -d $TAG_NAME

Push tags.
git push --tags

Checkout tag.
git checkout tags/$TAG_NAME


Move commit to another branch.

git checkout -b $BRANCH

git switch master
git reset --hard HEAD~1
git push -f

Fetch branch without checking out.
git fetch origin master

Prune local remote branches .
git remote prune origin

Pull all remote branches.

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

Delete all local branches except master.
git branch | grep -v "master" | xargs git branch -D


Save unstaged changes.

git stash
git stash apply

Stash a specific file.
git stash file path/to/file

Remove untracked local files and directories.
git clean -df

Stash with name.
git stash save "$DESCRIPTION"


Search the git log.
git log --all --grep='Build 0051'


Create local config.
git submodule init

Clone submodules.
git submodule update

Initialize and pull git submodules recursively.
git submodule update --init --recursive

Show remote of submodules.
git config --file=.gitmodules -e

Remove a submodule.

# Remove submodule from .git/config
git submodule deinit -f path_to_submodule

# Remove submodule from .gitmodule and remove directory 
git rm --cached path_to_submodule

# Remove submodule form .git/modules directory
rm -rf .git/modules/path_to_submodule

# Remove submodule
rm -rf path_to_submodule

Get current branch of submodules.
git submodule foreach 'git status' | grep 'On branch' -b1

Remove all submdoules.

# Deinit all submodules from .gitmodules
git submodule deinit .

# Remove all submodules (`git rm`) from .gitmodules
git submodule | cut -c43- | while read -r line; do (git rm "$line"); done

# Delete all submodule sections from .git/config (`git config --local --remove-section`) by fetching those from .git/config
git config --local -l | grep submodule | sed -e 's/^\(submodule\.[^.]*\)\(.*\)/\1/g' | while read -r line; do (git config --local --remove-section "$line"); done

# Manually remove leftovers
rm .gitmodules
rm -rf .git/modules


Setup merge tool.
git config merge.tool vimdiff

Show changed files only.
git diff --name-only HEAD HEAD~1

Show changes for one file only.
git diff HEAD HEAD~1 path/to/file

Show diff for commit and its ancestor .
git diff 0690a1046771702ec50c3ccf9e834d33debeb2f0^ 0690a1046771702ec50c3ccf9e834d33debeb2f0


Set default editor.
git config --global core.editor "vim"

Pull Requests

You can checkout a single pull request reference by doing.

# Replace $PR with the pull request number.
git fetch origin pull/$PR/head
git checkout FETCH_HEAD

If you want to fetch them all.

git fetch origin +refs/pull/*/merge:refs/remotes/origin/pr/*

For which you can then checkout the pull request by doing.

git checkout origin/pr/$PR

If you want to automatically fetch pull requests you can add it to your repo’s config.

git config --add remote.origin.fetch +refs/pull/*/merge:refs/remotes/origin/pr/*/merge
git fetch


Squal the last three commits.

git rebase -i HEAD~3

Then use keywords pick to select final commit and squash for the once to be merged.

Remove zip files from git history.

git filter-branch --tree-filter 'rm -f path/to/folder/*.zip' HEAD
git push origin --force --all

Categories: Software development
Tags: git
Improve this page
Show statistic for this page