10 Git Tricks Every Developer Should Know
Here’s ten tricks you can use in git to supercharge collaborative coding.
Access The In-Built Cheat-Sheet
Did you know git had its own in built quick reference guide that comes along with the installation package? Here’s how you can access it
git help everyday
This will open an html page in your default browser, you can use this as a quick reference guide to brush up on your git skills.
Commit Message With Summary And Description
Here’s how you can (should) format your commit messages for extensive readability. Keep in mind to follow the 50/72 rule to follow standards, i.e. summary should be no more than 50 characters and 72 for description.
git commit -m "<summary>" -m "<description>"
Search Older Commits By Commit Message
You can search for older commits using the following command. This can be particularly useful if you name your commit message with your JIRA ticket number.
git log --grep="<commit_message>
Find Who Edited What
If you feel like blaming someone else for some line of code which stopped working, there’s a git command for that
git blame <file_name>
This will print the current file with the commit id along with the developer’s name on the left side of each line of code.
For a better UX you can use the annotate
command in Android Studio for the same result
Set aliases for git commands
You can also set aliases for git commands if you don’t feel like typing too much
git config --global alias.<alias_name> <command_name>
Switch To Last Working Branch
Let’s say you’re working on your development
branch and your manager asks you to quickly get a build ready from the master branch. You take your stash, switch to master and build your apk, now to switch back you have to type in that long branch name again, worse if you don’t remember the name and you have to look it up. Here’s a shortcut you can use to ease your life
git checkout -
Auto Correct Typos
If you’re feeling brave, you can set git to autocorrect your typos. For example, stats
would get auto-corrected to status
. Bear in mind, some auto-corrects can be ambiguous and git uses its own algorithm to determine what would be the correct command for your typo.
git config --global help.autocorrect 1
Show File From Another Branch
Let’s say you’re working in your dev branch and you quickly need to refer some change which you did in another branch. Instead of switching branches, you can use this shortcut to see your file from the other branch
git show <branch>:<file_name>
Fix Typo In Last Commit Message
How many times has it happened to you that you made a typo in your commit message. Git allows you to easily fix that with the amend
command
git commit -v --amend -m "<updated_mesage>"
-v
is completely optional but I like the verbosity
Undo Last Local Commit And Keep Changes
Let’s say you made your commit after a day’s long work but you commited your code in another branch. Of course you wouldn’t want to lose all that work, here’s how you can revert that last commit while keeping your changes
git reset HEAD^