May 18, 2017 - 1 minute read

This is the post that I'll use as a living dumping ground for random useful git snippets that I come across. Enjoy!

Use git blame to see when a change was REALLY made, as opposed to just a whitespace or copy/paste commit:

git blame -w git blame -M git blame -C

Fetch a file from another branch without switching branches:

git checkout < OTHER_BRANCH > -- path/to/file.txt

Delete already-merged git branches (except for specific ones you name):

git branch --merged | grep -v "\*" | grep -v master | grep -v develop | grep -v release | xargs -n 1 git branch -d

View the git log for a specific line or number of lines in a file:

git log -L 1,1:some-file.txt

Search all commit diffs for a specific string - both regex and non-regex versions

git rev-list --all | xargs git grep '<YOUR REGEX>' git rev-list --all | xargs git grep -F '<YOUR STRING>'

View a diff on a specific file between the current branch and another branch

git diff some-other-branch some-filename.js

Move your last commit to a different branch, for when you accidentally commit to the wrong branch

git reset HEAD~ --soft git add . git stash git checkout name-of-the-correct-branch git stash pop git add . git commit -m "your message here"

Delete all untracked files, but do a dry run first:

git clean -fn

List the most recently updated branches: