Robert Eisele
Engineer, Systems Architect and DBA

Git Cheat Sheet

Create a local repository

Initialize a new local repository

git init

Clone an existing repository

git clone [email protected]:foo/bar.git

Local Changes

Show changed files in the working directory

git status

Show changes of tracked files

git diff

Add all current changes to the next commit

git add .

Add some file changes to the next commit

git add -p <file>

Undo git add without touching the file

git reset <file>

Commit all local changes in tracked files

git commit -a -m "<commit message>"

Commit all added files

git commit -m "<commit message>"

Commit History

Show all commits

git log

Show changes for a specific file

git log -p <file>

Figure out who changed a file

git blame <file>


List all existing branches

git branch

Switch to another branch

git checkout <branch>

Create a new branch

git branch <branch>

Create and switch to new branch

git checkout -b <branch>

Push a branch to a remote

git push <remote> <branch>

Delete a branch

git branch -d <branch>

Delete a branch remotely

git push <remote> :<branch>


Add a Tag, like v1.0.0, to mark the current commit

git tag <tag>

Push the new tag to a remote

git push <remote> <branch> --tags

Delete a Tag

git tag -d <tag>

Delete a Tag remotely

git push <remote> :<tag>

Remote Upstreams

Add a new remote upstream

git remote add <remote> <url>

Fetch all changes from remote

git fetch <remote>

Fetch and merge from remote

git pull <remote> <branch>

Push local commits to a remote

git push <remote> <branch>

List all currently configured remotes

git remote -v

Show information about a remote

git remote show <remote>

Merge and Rebase

Merge a branch into current HEAD

git merge <branch>

Rebase the current HEAD onto

git rebase <branch>

Abort a rebase

git rebase --abort

Continue a rebase after resolving conflicts

git rebase --continue

Open the configured merge tool to solve conflicts

git mergetool

After fixing a conflict manually, mark file as resolved

git add <file>
git rm <file>


Undo a previous commit

git reset --soft "HEAD~1"

Undo a previous commit already pushed to a remote

git push -f <remote> HEAD^:<branch>

Discard local changes

git reset --hard HEAD

Discard local changes in a specific file

git checkout HEAD <file>

Revert a commit

git revert <commit>

Reset HEAD pointer to previous commit and discard changes

git reset --hard <commit>

… and preserve changes as unstaged

git reset <commit>

… and preserve uncommitted local changes

git reset --keep <commit>

Find and restore a deleted file

git rev-list -n 1 HEAD -- <file>
git checkout <commit>^ -- <file>