top 5 favorite git tricks
¶ by Rob FrieselI’ve been using Git as my VCS-of-choice for a few years now. It’s a powerful tool for source control, and like so many other awesome tools: it takes minutes to learn, and a lifetime to master. If you’re new to Git, there’s a fantastic round-up of tutorials and resources over on Six Revisions; or if you want my advice: (1) Github can teach you the basics in 15 minutes; (2) follow along with Git Immersion to get to the next level; and (3) refer to Pro Git while you ascend to master.
But this post isn’t about the basics. This is about my five favorite tricks with Git — the ones that I use every single day. (And these don’t even require tapping into its plumbing. 1) So without further ado…
1. Instant Changelog
git log develop --no-merges --not master |
In plain English? “Get me the log of all the commits in develop
that are not in master
, hold the merge commits.”
2. Current Commit?
git log -n 1 --abbrev-commit |
In plain English? “Show me the most recent commit. And just the short SHA, please.” I used this so much that I mapped it in my ~/.gitconfig
to curr-commit
.
3. Interactive Add FTW
git add -i |
Unless it’s an untracked file, I basically never add anything without going through the interactive add mode. It is absolutely the best way to review the changes you’re about to stage/commit. I’ve caught so many left-over debugging statements this way. (James Cash has a good, four-minute intro video to git add -i
.)
4. Infinite Tag Sort
git tag | sort | |
# or since `sort` doesn't deal with '2.2.1' vs. '2.2.11' too well: | |
git tag | sort-script.rb |
As I’ve written about before, if you work on a reasonably “big” project that has lots of tagged versions, then you can wind up with an unwieldy list. And since Git puts them in alphabetical order, you’ll want to pipe them to sort
. But depending on your versioning scheme, you might not see that there’s a version 6.5.11.21
because 6.5.11.9
is the last one you see in the output. Luckily, there are a couple solutions for this problem.
5. “Remove, but don’t delete.”
git rm --cached some_file_accidentally_checked_in |
In plain English? “Remove that file from the index, but don’t delete it off the filesystem.” (Hat tip: Matt Bindoff, citing this Stack Overflow thread.)
These are just my top five. What are your favorites? What “Git tricks” get you through the day?
Leave a Reply