I've been using git for over 8 years now and I never saw these features until recently. I decided to create this article since most of these features I found out about on random comments of random articles over the web. I believe this is a first time for you as well :D
Did you know that you can create templates for your git commits?
git commit --template=<file>
You can use a template file for all your commits, heck, you can even set a configuration variable for this template file using the key commit.template
Let's use this template as example:
Now you can set this template to be used every time you type
git commit, by running
git config commit.template /template/path. If you want to allow empty commit messages, you should use
git config commit.cleanup strip otherwise it will commit the template as a message (if yours have uncommented lines). PS: you can always use
--global to set for all your repositories.
Did you know you can use
git commit --amend to redo your latest commit?
git commit --amend
I've always used this command to fix a typo in the message of my current hash, until one time I accidentally committed, modified a file by mistake and ran this command. If you take your time to read the documentation, you'll see that this command is roughly the same as doing
git reset --soft HEAD~1, adding your working tree contents to your staging area
git add -A . and committing
git commit -c ORIG_HEAD. Beware that you're rewriting your repo history.
Did you know you can cherry-pick to your staging area?
git cherry-pick -n <HASH>
One time I was cherry picking a branch of another dev and I needed to fix a typo in one of his file comments without needing to add another commit to my repo tree. This essentially copy all contents from the given hash to your staging area.
Did you know you can make git solve conflicts for you if you know what you're doing?
git merge <branch> [-Xours] [-Xtheirs]
This is quite useful when your current branch is ahead of the one you're merging with and you know you modified a specific part of the code that yours or theirs is the correct version.
Did you know you can find out a bug faster using binary search?
git bisect [start] [bad] [good <HASH>]
I use this occasionally when I realize that a feature is broken after a handful of commits in which includes good work. To help you finding which hash contains your bug, you can use
git bisect to do a binary search instead of a linear search done manually.
You start your search by running
git bisect start. Then, you tag your current hash as a bad hash with
git bisect bad. Finally, you tag your latest good revision by running
git bisect good <HASH>. This will start an interactive bisect and checkout to a commit that git thinks it contains the bug. Now you can build & test it and tag if this commit is good or bad.
Here we have a full
git bisect run and successfully finding us our bad commit (it only took two builds to find it, contrasting with the five ones we would've done manually):
Did you know you can find out when a specific function or class was introduced?
git log -Gword --reverse
This command is helpful when you want to know when a new function, class, variable or whatever was introduced in your code. When you find out the first hash that contains this word, you can
checkout this hash and do your work. If you don't use the
--reverse flag, it will show all changes dated by the most current one.
Tips & Tricks
This is a beautiful way to visualise your git log:
These are quick shell scripts to your pull & push operations:
This is a quick way to get rid of a branch locally and on your remote: