Git is probably the greatest thing that happened to the world of programming since the invention of sliced bread (1928...wait, seriously?). Anyway, as I'm sure we all know sometimes it can also be a huge pain in the butt. So here's a handful of quick tips and tricks that make it less so.
> List of commits that have already been cherry picked
I'm a fan of trunk based development, I find it to be a nice way to handle complexity in configuration management.
But it also means I cherry-pick a lot. And so it's pretty useful to know what commits I've already copied onto the target branch.
To do just that, run git cherry -v <target_branch> <source_branch>
. Each line beginning with the +
sign is a commit that hasn't been picked into target branch (while -
means it's there).
> (So Much) Nicer commit graph
git log
is boring. And verbose. And uses too much unnecessary whitespace. And I can't see what's where with regards to branches. We can do better. Much better.
Like git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev -commit --date=relative
better.
Seriously, try it and you'll never use git log
ever again.
> Stored-my-code-in-a-branch-and-forgot-which-one
Now, this happens to me regularly: I implement a new feature in a branch and forget the name immediately. Or definitely the next day. And since there's twenty other (mostly legacy) branches in my git which I haven't merged in and haven't cleaned up because, well, I'm a lazy bum, it's hard to find it again.
This command will list your branches sorted by the last commit's date (starting with the newest one).
git for-each-ref --sort=-committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color: reset))' | less -R
Kudos Jakub Narębski!
> Last minute check before commiting
Super simple but super useful one. Shows the diff at the bottom of the commit message (don't worry, git will strip it for you before storing it). You wouldn't believe how often you can catch a bug just by glancing at your patch right before you commit.
git commit -v
Happy gitting, folks! I'm @tomas_brambora.