Chris's coding blog

Git cheatsheet for ninjas

January 08, 2016

One of my favourite parts of working with Mercurial for 2 years was its command line, and committing and updating through it. I’m using Git now rather than Mercurial, but the same principal still applies for me - command line first, and UI mainly for diffs. I prefered Mercurial’s diffing and branching but on the whole you can go between the two fairly effortlessly.

Below is a list of advanced Git usage I’m accrueing, largely thanks to the Git ninjas I work with, Stackoverflow and a tiny amount of self-discovery.

# Change the config editor to notepad
git config --global core.editor notepad
# ...and then edit the config
git config --global --edit
# Revert master back by 5 commits
git revert --no-commit HEAD~5..
# Revert master back by 5, but do it so there you get a "Revert commit" for each commit you reverted (this opens Vim)
git revert master~5..master
# Display the last 3 commits as single lines, abbreviate the SHA
git log -n 3 --pretty=oneline --abbrev-commit
# Find a commit e.g. delete
new-alias grep select-string
git log --format=oneline --abbrev-commit | grep myfile.js
# Ignore a file (that's been added prior to a .gitignore)
git update-index --assume-unchanged path/to/file
# Un-ignore that file
git update-index --no-assume-unchanged path/to/file
# Reset all staged checkins
git reset --hard
# Clean all unstaged checkins
git clean -n
# Clean all
git clean -xcd
# Turn all uncommitted changes into a new branch
git checkout -b <branchname>
# Find list of changes for a directory
git log --name-status -10 path/to/dir
# Remember usernames/passwords in Git extensions.
# Add this to your .gitconfig file
[credential]
helper = !\"C:/Program Files (x86)/GitExtensions/GitCredentialWinStore/git-credential-winstore.exe\"
# List all remote branches
git remote show origin

gitcheatsheet

I'm Chris Small, a software engineer working in London. This is my tech blog. Find out more about me via GithubStackoverflowResume