Top Ten Git Tips & Tricks

Let's face facts. Git is not fun. Git is not friendly. No. It's just infuriatingly useful, so we're stuck with it. But what if you could make git more friendly? More convenient? Would that make your day a little less stressful? In this article, Julie Kent shows us how we can do this with just a few simple tweaks.

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=oneline --graph --decorate --all

Here's an example of what you can expect:

example

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 fetch or pull.

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

Using 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)

5. Advanced git add

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:

exampleadd

4. Speaking of that p flag

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

3. Automate git bisect

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

git help

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.

Wrapping Up

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!

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Julie Kent

    Julie is an engineer at Stitch Fix. In her free time, she likes reading, cooking, and walking her dog.

    More articles by Julie Kent
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like:

    “Everyone is in love with Honeybadger ... the UI is spot on.”
    Molly Struve, Sr. Site Reliability Engineer, Netflix
    Start free trial