Git is one of the most widely used collaboration tools in Software development. Even software developers working without teams often use Git as a version control system. Most people interact with Git through the command line, but many code editors and IDEs have Git integrations with built-in tools to make your workflow easier. Though Git is widely used, many developers only have a surface-level appreciation of all that Git has to offer.
If you already have an understanding of how Git works, this article will help you level up your skills and become a better collaborator.
Since you're here, you're probably ready to go beyond the simple (but complete!) workflow of:
git add .
git commit -m "fix"
git push
Hopefully, these Git tips and tricks will increase your efficiency and productivity as a developer. Getting better at any tool you have to use as often as you do Git will help you spend more time coding. Wouldn't it be great to spend less time trying to decide whether to git merge
or git rebase
? Without further ado, let's get into it!
10. Make empty commits
Have you ever found yourself making a small tweak to the README so that you can kick off a CI 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 helpful command:
git commit --allow empty -m 'it works!'
This git flag allows you to trigger a commit with no content, skipping the error you would usually see that says you have nothing staged. This trick is especially useful to kick off a workflow without having to make a trivial change to a README or some other file. The git diff
will show no changes, but the commit is helpful when you need to kick off a CI run or even a production deployment without having to push arbitrary code.
9. Make your log pretty!
Running git log
is a super useful way to view recent changes to a codebase, but your eyes quickly get lost if you run it on a project with a lot of branches. Another git trick is to make the git log
more fun, adding a bit of color to help your eyes and brain read what's on the screen better.
For a better git log
without any changes to your Git config, just run:
git log --pretty=oneline --graph --decorate --all
Here's an example of what you can expect:
You'll notice both the color coding and the clever use of | and \ to indicate branching. Though these flags don't functionally change anything in the log, they certainly make understanding the output of git log
a lot easier.
8. Clean up your local branches
If you're like me, you enjoy keeping your workspace relatively tidy, and this includes your computer.
Developing software can quickly leave you with a mess, especially if you're working across a lot of repositories. This is only compounded by using Git, which can leave each repository with a number of branches long after those branches are no longer needed.
Over time, you have probably added different things to your Git config to customize your experience. I fell in love with the git config
setting that deletes any local branch that has been removed from the remote repository when doing a fetch
or pull
. You can quickly set this setting by running:
git config --global fetch.prune true
Another Git tip beyond changing your Git config is to take this a step further by deleting local branches that have already been merged into the master branch by running this Git command:
git branch --merged master | grep -v "master" | xargs -n 1 git branch -d
7. Bring back a deleted commit with Git reflog
Using git rebase
is extremely valuable, especially when run with the --interactive
flag. Still, rebasing can be dangerous. When you rebase locally, you'll have to force push to overwrite changes on the remote repository. If you rebase often enough, you will eventually accidentally rebase away a commit (or several).
If you're as unlucky (or uninformed) as me, you might have even accidentally deleted a week of work. One of my favorite Git tips is to use git reflog
to bring missing commits back from the dead.
As long as you've committed your work, it still lives in your local working copy regardless of what you've done with a git rebase
. 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 local branch. Crisis averted!
6. Alias Git blame
Every once in a while, there's a bug you can't figure out on your own. Sometimes, you want/need to reach out to the person who wrote the code that does not appear to be working. You may even just want to talk to the author to better understand the context behind the code.
It's common to use git blame
to figure out who wrote a line of code. In fact, git blame
shows you who wrote a line of code, when they wrote it, and in what commit it got added to the codebase. Many code editors and IDEs build this functionality into the editor, displaying names next to each line of code!
This is an underrated feature of Git, but running git blame
just doesn't feel very nice. It's important that software engineering teams cultivate a blameless culture, and the phrasing of this command might set the wrong precedent.
Good news! You can actually change a Git command using an alias in your Git config, the most selfless of these Git tips. To alias git blame
to instead be invoked by git investigate
, run this command:
git config --global alias.investigate blame
You can change investigate
to whatever word you'd like! Your Git config can also hold Git aliases for other commands, so you can extend this beyond git blame
if you like.
5. Use git add -p
to stage commits (the most time-saving of these Git tips)
At the beginning of the article, I promised you we'd move past git add .
.
You probably already know that you can stage all changes in a local repository with git add .
You probably also know that you do this in a more verbose way by viewing what files have changed with git status
, checking a file's changes with git diff <filename>
, and staging files individually with git add <filename>
. This is fine if you are staging a single file, but can quickly become a pain when you have a bigger set of local changes.
This option is more verbose, but it's an important step to validate that what you're staging is local changes you actually intend to commit. Nothing is worse than committing something by accident, especially if it contains sensitive data like an application secret. Fortunately, there's an even better way to stage changes!
You can piecewise stage changes by running:
git add -p
This is probably the most time-saving of the Git tips in this article. Staging Git commits in this way opens an interactive prompt that shows you sections of each file that have been changed, giving you the option to stage, skip, and more.
This makes it easier to stage changes while actually validating each change, but it's also helpful if you only want to commit a fraction of a file's changes. 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.
Here is an example:
4. Piecewise stash code
The piecewise -p
flag is not just for staging code! It can also be used with the git stash
command when you don't want to git stash
all files or the entirety of one file with changes.
To git stash
code in your working directory piece-by-piece, run:
git stash -p
When running git stash -p
, you will see a similar interactive screen as git add -p
, stepping you through each change one section at a time.
3. Automate git bisect
While this is the most challenging of the git tips and tricks, automating git bisect
can pay dividends over time. If you already understand how to use git bisect, you’ll know that using git bisect
can require a lot of commands, which can limit its practicality.
Because of this complexity, you might be interested to know that you can actually write a script that can automate some of this process. Once you write a git bisect script, you can then use this simple command to run your script.
git bisect run my_script arguments
2. Use emojis in Git
While this is probably the least immediately useful of these Git tips and tricks, using emojis in commit messages is an easy way to add some fun to your and your coworkers' day!
You can easily insert emojis into commit messages provided you know them by name. You can reference the Gitmoji cheatsheet to pick a relevant emoji, and insert it as text in your commit message like this:
git commit -m ":package: Add a docker container to the app"
1. Reference Git documentation
I'm always amazed by how quickly I forget about the built-in help tools in a number of applications and command-line interfaces. I'll be trying an endless number of Stack Overflow answers to solve my problem, and finally, I'll remember I can get help by simply running:
git help
You essentially have all of the Git documentation at your fingertips as part of the Git CLI! If you're completely new to Git or just want a nice refresher, you can even get to a tutorial by running:
git help tutorial
This tutorial will run you through the basics.
Finally, if you want to get documentation for a specific command, you can use the man
command for the manual. For example, man git-log
if you want to know more about the git log
command.
Finding even more Git tips
Well, there you have it, my ten favorite Git tips and tricks. Whether it's customizing your Git config, staging changes with git add -p
, using -p
with git stash
, or using the Git reflog, the tricks you learned today will make Git more useful to you and your team. I hope that you've at least come across something new that you didn't know before or perhaps 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. If there's a Git trick that you find helpful, share it with your peers!