Git Quick Reference
I use git every day, but there are some things I need to do infrequently enough I often forget how. Here they are for reference. If you're learning git, try an integrated reference first; Git Magic is my favorite.
☙ Undo last commit
For mistakes.
git reset HEAD~1
Please don't try to do this after pushing.
☙ Revert a pushed commit
The goal here is not to pretend a commit never happened, but to record reversing it.
git revert [commit-hash]
From here.
☙ Git Pickaxe
This searches all past commits (including diffs) for the specified string. The -i
makes the search case insensitive.
git log -i -S[word]
This will pop up the log of all commits that match, and is useful for tracking down when a variable first or last appeared.
☙ Find when a file was deleted
Where did that config file go?
git rev-list -n 1 HEAD -- [path]
From here.
☙ Make an annotated tag
Used for marking releases.
git tag -a v0.0
This opens an editor and is a good place to put change notes.
☙ Deleting remote tags
I had to do this when I inherited a project with hundreds of tags nobody cared about. This will delete all tags, though by dumping git tag
to a file you can of course modify the list manually.
git show-ref --tags -d > old-tags # save them just in case
for tag in $(git tag); do
git push origin :$tag
git tag -d $tag
done
With only current tags the release list was useful again.
☙ Tracking a remote branch
For sharing changes.
git checkout --track origin/[branch]
Ψ