This article complements ‘Understanding How Git Works’. Now that you have an understanding of the plumbing, it's time to level up your skills. Hopefully, these tips and tricks will increase your efficiency and productivity as a developer. It will help you spend more time coding and less time trying to decide whether to
git merge or
git rebase. Without further ado, let's get into it!
10. Blank commits
Have you ever found yourself making a small tweak to the README so that you can kick-off a build (or some other integration) and try to debug an issue? I used to do this fairly frequently, until I found out about the following command:
git commit --allow empty -m 'it works!'
This allows you to trigger a commit and kick off a workflow without having to make a trivial change to a README or some other file.
9. Make your log pretty!
This one is more fun, and sometimes, adding a bit of color does help our eyes and brain read what's on the screen better.
git log --pretty=online --graph --decorate --all
Here's an example of what you can expect:
8. Clean up your local branches
If you're like me, you enjoy keeping things tidy, and this includes your computer. :) I fell in love with the
git config setting, which deletes local branches that have been removed from the remote when doing a
git config --global fetch.prune true
Similarly, you can delete local branches that have been merged into the master by running this code:
git branch --merged master | grep -v "master" | xargs -n 1 git branch -d
7. Rebase oopsie
git rebase is an extremely valuable command, but sometimes, you accidentally rebase away a commit and start sweating. Or, maybe that's just me. :)
git reflog to the rescue! As long as you've committed your work, it still lives in your local working copy. Using
git reflog, you can find the SHA1 that you need something from. Then, run
git checkout <SHA1>, copy what you need, and run
git checkout HEAD to return to the most recent commit in the branch. Crisis averted!
6. No more blaming!
There's a bug, and you can't figure it out. Sometimes, you want/need to reach out to the person who wrote the code that does not appear to be working. You run
git blame, which will show each line's last commit change and who changed it. Cooooool, but ugh, running
git blame just doesn't feel very nice. Good news! You can change the alias using this command:
git config --global alias.investigate blame (You can change
investigate to whatever word you'd like)
Sometimes, I get carried away to the point that I realize I have way more changes in a file than make sense to commit together. I recently learned that you can use
git add -p to selectively organize your commits. Here is an example:
4. Speaking of that
It can also be used with
git stash when you don't want to stash all files or the entirety of one file with changes. When running
git stash -p, you will see a similar interactive screen as
git add -p
This is more of an advanced tip. If you haven't heard of
git bisect yet, I recommend reading this blog post. After reading the blog post, you’ll know that using
git bisect can require a lot of commands, which can limit its usefulness. Because of this complexity, you might be compelled to write a script that can automate some of this process. You can then use this nifty command to run your script.
git bisect run my_script arguments
You can include a number of arguments. Read more about
git bisect and how to use the
run command here.
2. Have some fun
Make use of emojis by bookmarking this cheatsheet.
1. Need help?
I'm always amazed by how quickly I forget about the built-in help tools within a number of applications and CLI's. I'll be trying an endless number of Stack Overflow answers to solve my problem, and finally, I'll remember
You essentially have all of the Git documentation at your fingertips! If you're completely new to Git or just want a nice refresher, you can even run
git help tutorial
which will run you through the basics.
Finally, if you want to get documentation for a specific command, you can run, for example,
man git-log if you want to know more about the
git log command.
Well, there you have it! I hope that you've at least come across something new that you didn't know before or refreshed your memory about a command you might have forgotten about. Git is an amazingly powerful tool, and we should make the most of it!