raw cheat sheet

Git Cheat Sheet

Robert Eisele

Git configuration

Set the name that will be seen in commits and tags

git config --global user.name "Robert Eisele"

Set the E-mail address that will be attached to commits and tags

git config --global user.email "robert@example.org"

Enable colorization of git output

git config --global color.ui auto

Starting a project

Initialize a new local repository in the current folder

git init

Download and clone an existing repository with the entire history from an URL

git clone git@github.com:foo/bar.git

Local Changes

Display changed files in the working directory

git status

Show changes of tracked files in the working directory and the staging area

git diff [file]

Show changes between the staging area and the repository

git diff --staged [file]

Add all current changes to the staging area for the next commit

git add .

Add some file changes to the staging area for 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>

Branches

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>

Tags

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

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>
or
git checkout -- <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>