Git cheat sheet

Here are the git commands I most frequently use, along with a brief description of what they are used for.

Setting up

  • git init: create a new repository of the current directory.

Creating commits

  • git status: show the current status of the git repository, along with staged and untracked files.
  • git add <filename>: add <filename> to the staging area (files to be committed). git add . will add all untracked files.
  • git rm --cached <filename>: unstage <filename>
  • git commit: commit staged files to the repository. Use -m option to specify commit message directly.

Working with past commits

  • git log: show a list of all the past commits.
  • git log --oneline: show a one-commit-per-line list of al past commits
  • git reset <mode> <commit>: reset the current branch head to the specified commit. Commits can be indicated with their hash or with notation like HEAD~1 ( = the current HEAD commit’s parent) and HEAD~2 ( = parent of HEAD~1). There are different ways of going about this, given by the <mode> option.
    • --soft: don’t change the working tree, just move HEAD. This leaves all changes since the specified commit as “changes to be committed”.
    • --mixed: default behavior. Reset the index but not working tree, preserving the changes but not staging them for commit.
    • --hard: Reset the entire working tree and index – any changes that were made are discarded.
  • git clean -fdx: remove untracked files from the working tree.
    • -f: force.
    • -d: directories too.
    • -x: ignore the directives in the .gitignore file (will delete files specified there too).
    • -n: dry run. Only show filed that would be removed but don’t remove them yet.
  • git show --stat <commit>: show which files have been changed in the specified commit, in a similar format to when a commit is made. If you don’t specify a commit hash, the latest commit will be shown.

  • git show <commit>: shows all the changes that happened in each file for the specified commit. If you don’t specify a commit hash, the latest commit will be shown.
  • git diff <commit1> <commit2>: show difference between commits or between a commit and the working tree (when only one commit is given). A simple git diff will show the difference between the latest commit and the working tree.

Pushing, pulling and cloning

  • git fetch: sync with the remote server, getting information about remote commits but not downloading any data.
  • git pull: update the repository by downloading updates from a remote source. --allow-unrelated-histories may be necessary.
  • git push: update the repository on the remote server by uploading commits made on this machine.
  • git clone <repo>: download a new repository from the remote server.
  • git push <remote> --mirror: push all branches, tags and remotes to the remote.

Remotes and branches

  • git remote add <remote-name> <location>: create a new remote.
  • git remote update: bring your remote refs up-to-date, showing you if something happened upstream (like, for instance, a new branch was created).
  • git remote show origin: show all branches at the origin and the ones configured to match them on the local machine.
  • git branch -a: show all branches, even “hidden” ones on the remote.
  • Automatically create a local branch for each remote branch:
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

Updating a forked repository

If you have forked a Git repository and would like to update your fork with commits from the original repository, you will need to add it as a remote, then rebase (or moerge, but that will create an extra commit message) your fork onto it:

$ git remote add upstream git://
$ git fetch upstream
$ git rebase upstream/master
$ git push --force upstream/master