Git Cheatsheet

xkcd: Git


  • git checkout -b <new-branch-name> : get a new branch and switch to it
  • git checkout -b <branch-name> : get a local copy of a remote existing branch
  • git checkout <branch-name> : switch to an existing branch
  • git checkout --orphan <branch-name> : create branch without full history
  • git log -n X -p : show X last commits with diffs
  • git log <branch-A-name> ^<branch-B-name> : commits in branch A that aren't in branch B
  • git log -- <path>: filter to commits including modifications to <path>/file
  • git log --pretty=oneline --stat --all <folder-name> : same as previous, but better presented
  • git log --format='%H' -1 HEAD: Get last commit hash from current branch
  • git log --pretty=format:"%h %s" --graph: format the commits log as nice graph with hash & commit message
  • git log -S <string>: find all commits that modified occurrences of <string>, e.g. git log -S myFunction
  • git fetch
  • git pull origin <branch-name>
  • git pull --rebase: Avoid having a merge commit if you had changes and origin had commits too (rewrites your local history)
  • git merge --no-ff <source-branch> : merge into current branch source one without rebase/FF
  • git push origin <destination-branch> : push merges/changesets to a branch
  • git push origin <destination-branch> --force-with-lease : push merges/changesets to a branch. If remote had different commits not in local, fails and does not push. Safer than --force which would override remote commits.
  • git remote -v: show all remotes configured in the repository
  • git remote show origin : display the path of the repository
  • git remote set-url origin xxx : Change remote URI to xxx
  • git remote rm <name> / git remote remove <name> : Remove a remote
  • git remote rename <old> <new>: Rename a remote (just the name, not the URI)
  • git remote add origin<path>.git: replace the origin remote URI. Need to firstgit remote rm origin to remove the previous one.
  • git remote add <user><user>/<path>: add <user> fork to a repository as a remote. When pushing changes afterwards to that remote, Github will propose you to create a pull request directly from the fork remote towards the original remote.
  • git pull <user>: fetch branches from remote <user>.
  • git add xxx : add files (use . for everything, <folder>/.. for folder recursive children)
  • git commit : commit changes
  • git status : show status of uncommited files
  • git checkout <filename> : revert a file
  • git checkout <branch-name> <filename> : Checkout all changes to <filename> from branch <branch-name> into current
  • git checkout <revision> . : revert a full branch to specified revision if not commited
  • git revert <commit1> <commit2> ...: Reverts certain commits if commited
  • git clean -n: dry run to check which uncommited modifications would be removed
  • git clean -f -d : remove all local uncommited modifications
  • git branch : display local branches, active one is with an *
  • git diff : Show changes in files
  • git diff <branch-name> origin/<remote-branch-name> : show a diff between a local branch and a remote one from the origin remote
  • git --no-pager diff --name-only master...HEAD: list all modified files in commits from current branch in regards to master up to your last pull
  • git rev-list origin/master...master: list all commits that exist on local master but not on origin/master
  • git rebase <branch-name> : rebases current branch with specified branch (fetches remote branch changes and then adds your changes at the tip)
  • git rebase -i <branch-name>: rebases current branch with specified branch, allowing you to reorder changes
  • git rebase -i HEAD^3: allows you to check and reorder, mix or remove the last 3 commits from the current branch. Very useful to rewrite local commits, squash some of them, etcetera, before preparing a branch for review.
  • git rm <filename> : delete a file from branch and filesystem
  • git branch -d <branch-name> : delete a local branch
  • git push origin --delete <branch-name> : delete a remote brach
  • gitk <filename> : show visual git log
  • git reset <revision-hash> .: revert a full branch to specified revision if commited
  • git reset --soft HEAD~1 : reset to last commit (even if pushed). Can re-commit stuff but if already pushed will need to push with --force.
  • git reset --hard HEAD^: forcibly resets to the branch's head, discarding any commit.
  • git reset --soft <new-root-hash> && git commit --amend -m "<new message>" && git push --force: squash all branch pushed commits previous to the one specified into a single commit with the desired new message
  • git reflog: show a history of changes in all branches
  • git reset 'HEAD@{1}': revert to the previous command's commit. Check the reflog to see which command was last. This is very useful to undo non-trivial commands like a git reset HEAD~1.
  • git reset HEAD@<commit>: revert to a specific commit (often from the reflog)
  • git log origin/<branch-name>..<branch-name> : Show diff between local commits and remote commits
  • git submodule update --init --recursive : Init and update all submodules
  • git submodule init && git submodule update : Retrieve and update all submodules (alt)
  • git submodule foreach --recursive git reset --hard: Reset all submodule changes
  • Switch from current branch having a submodule to a branch without it:
rm -Rf <submodule-folder>
git reset && git checkout .
git checkout <branch-name>
  • git pull<username>/<repository-name>.git <branch-name>: Merge a pull request to local branch
  • git stash: Stash current changes
  • git stash apply: Unstash and merge stored changes
  • git checkout --theirs xxxx git checkout --ours xxxx: Keep changes from incoming branch or local one, respectively.
  • git blame -M: Blames original commit, not the move commit
  • git blame -CCC: Looks at all commits in history
  • git cherry-pick <commit>: merges and commits a specific commit to current branch. Note that SHA of the commit will change (as the date changes).
  • git commit --amend: Squash a change on previous commit and change the commit message
  • git diff --staged: Show both staged and unstaged changes that you will commit
  • Undo a commit removing it from history:
git reset --hard HEAD~1
# your new commit here
git push origin <branch> --force
  • Tag any commit of a repo (e.g. with a certain version):
git tag <label> <commit-id>
git push origin <label>
  • git tag -l "v1.*": Searching for tags supports wildcard via the -l option
  • Apply a .diff file: git apply <filename>.diff
  • Push to a different remote branch: git push origin <localb-ranch>:<remote-branch>
  • Rename a branch: git branch -m <old-name> <new-name>
  • git commit --no-verify ...: With great power comes great responsibility. This flag disables all commit hooks, so use it only when really in need.
  • git fetch origin master && git rebase origin/master && git push origin HEAD --force-with-lease: Rebase master with any new remote change, and then push, but not forcing if there is any kind conflict with the push. Also works if there's any amended commit.
  • git -C <path> <command> ...: Run git commands as if current folder was <path>


  • git config --list : List currently setup config values
  • git config --global "<username>" : Setup global user name to <username>
  • git config --global "<email>" : Setup global user email to <email>
  • git config --global credential.helper 'cache --timeout=28800' : Make git cache credentials for 8 hours
  • git config --global color.ui true : Activate colors in diffs, etc.
  • git config --global core.autocrlf input : Fix Convert newlines to Unix-style ones (Unix)*
  • git config --global core.autocrlf true : Fix Convert newlines to Unix-style ones (Windows)
  • git config --global core.excludesfile ~/.gitignore : Instruct git to always ignore patterns defined at ~/.gitignore
  • git config --global pager.log 'diff-highlight | less': Better diff highlighting (same for 3 following options)
  • git config --global 'diff-highlight | less'
  • git config --global pager.diff 'diff-highlight | less'
  • git config --global interactive.diffFilter diff-highlight
  • git config --global core.editor <editor-name>: change default editor (for commit messages, etcetera)



Third-party Tools

  • tig: to navigate commits & branches
  • Github:
  • hub: CLI tool to better interact with Github from the command-line
  • Commands for automatically closing tickets when merged to default branch
  • Search to display all issues/PRs of an organization:
  • Additionally, filter to things assigned to me or involving me:


Git conceptual model -

Git Cheatsheet. Author: