Manoj Rao bio photo

Manoj Rao

Your Average Common Man

Email Twitter Github

In the past, I wrote about using Magit in my Emacs workflow which helped me tremendously. This time, I want to dump all the git commands that I use frequently that were gradually stolen curated from the Internet over time.

Here’s my cheat sheet of git commands. By simply going through the list you will learn much more than you need for day-to-day activities.

But it also helps you get a step closer to becoming a git Guru.

Git cheat list

  • name of the current branch and nothing else (for automation)
git rev-parse --abbrev-ref HEAD
  • all commits that your branch has that are not yet in master
git log master..<HERE\_COMES\_YOUR\_BRANCH\_NAME>
  • setting up a character used for comments
git config core.commentchar <HERE\_COMES\_YOUR\_COMMENT\_CHAR>
  • fixing `fatal: Could not parse object` after unsuccessful revert
git revert --quit
  • view diff with inline changes
git diff --word-diff=plain master
  • view quick stat of a diff
git diff --shortstat master
git diff --numstat master
git diff --dirstat master
  • undo last just made commit
git reset HEAD\~
  • list last 20 hashes in reverse
git log --format="%p..%h %cd %<(17)%an %s"--date=format:"%a %m/%d %H:%M" --reverse -n 20
  • list commits between
git log --format="%p..%h %cd %<(17)%an %s"--date=format:"%a %m/%d %H:%M" --reverse --after=2016-11-09T00:00:00-05:00 --before=2016-11-10T00:00:00-05:00
  • try a new output for
git diff --compaction-heuristic ... --color-words ...
  • enable more thorough comparison
git config --global diff.algorithm patience
  • restoring a file from a certain commit relative to the latest
git checkout HEAD\~<NUMBER> --<RELATIVE\_PATH\_TO\_FILE>
  • restoring a file from a certain commit relative to the given commit
git checkout <COMMIT\_HASH>\~<NUMBER> --<RELATIVE\_PATH\_TO\_FILE>
  • restoring a file from a certain commit
git checkout <COMMIT\_HASH> --<RELATIVE\_PATH\_TO\_FILE>
  • creating a diff file from unstaged changes for a *specific folder*
git diff -- <RELATIVE\_PATH\_TO\_FOLDER>changes.diff
  • applying a diff file
  • go to the root directory of your repository
git apply changes.diff
  • show differences between last commit and currrent changes:
git difftool -d
  • referring to:
  • last commits `… HEAD~1 …`
  • last 3 commits `… HEAD~3 …`
  • show the history of changes of a file
git log -p -- ./Scripts/Libs/select2.js
  • ignoring whitespaces
git rebase --ignore-whitespace <BRANCH\_NAME>
  • pulling for fast-forward only (eliminating a chance for unintended merging)
git pull --ff-only
  • list of all tags
git fetch git tag -l
  • archive a branch using tags
git tag <TAG\_NAME> <BRANCH\_NAME>
git push origin --tags
you can delete your branch now
  • get a tagged branch
git checkout -b <BRANCH\_NAME> <TAG\_NAME>
  • list of all branches that haven’t been merged to master
git branch --no-merge master
  • enable more elaborate diff algorithm by default
git config --global diff.algorithm histogram
  • list of all developers
git shortlog -s -n -e
  • display graph of branches
git log --decorate --graph --all --date=relative
or
git log --decorate --graph --all --oneline
  • remembering the password
git config --global credential.helper store
git fetch
the first command tells git to remember the credentials that you are
going to provide for the second command
  • path to the global config
\~/.gitconfig
  • example of a global config\ \
  • viewing differences between current and other branch
git difftool -d BRANCH\_NAME
  • viewing differences between current and stash
git difftool -d stash
  • viewing differences between several commits in a diff tool
git difftool -d HEAD@{2}...HEAD@{0}
  • view all global settings
git config --global -l
  • delete tag
git tag -d my-tag git push origin :refs/tags/my-tag
  • pushing tags
git push --tags
  • checking the history of a file or a folder
git log -- <FILE\_OR\_FOLDER>
  • disabling the scroller
git --no-pager <...>
  • who pushed last which branch
git for-each-ref --format="%(committerdate)%09 %(refname) %09 %(authorname)"
  • deleting remote branch

    bash Emacs git push origin :<BRANCH_NAME>

  • deleting remote branch localy

    bash Emacs git branch -r -D <BRANCH_NAME>

    or to sync with the remote

    bash Emacs git fetch –all –prune

  • deleting local branch

    bash Emacs git branch -d <BRANCH_NAME>

  • list *actual* remote branchs

    bash Emacs git ls-remote –heads origin

  • list all remote (fetched) branches

    bash Emacs git branch -r

  • list all local branches

    bash Emacs git branch -l

  • find to which branch a given commit belongs

    bash Emacs git branch –contains <COMMIT>