Jun 23, 2015

Getting smarter with git

I generally do all my work with git on the command line — sometimes I do commits in my IDE but all my pushes, pulls, merges, and looking at history is done on the command line.

First off, I have a command prompt that tells me what branch I’m in. I use zsh but you should be able to find a similar one if you use bash. I will warn you that it’s really slow in Cygwin but in “pure” Unix systems like OSX and Ubuntu it works fine.

After that, I really only have 4 magic git commands:

The git smart-* commands are from the git-smart Ruby gem. You can read all about it here. These commands have helped me become more productive and save me so much time.

Getting status

My gss command is one that I run the most often. It’s my default look into what has changed locally. It’s a quick view, and it tells me what I need, short and sweet:

git-status

Looking at Commit Logs

I use gl as my lifesaver on what has changed and when. My favorite part is how it tells you what was committed in each branch and the life-cycle of branches. And you can easily see when branching and merging went really, really wrong.

when_branches_go_wrong

Yes, that’s a real log from a real git project I was in a long time ago. And, yes, it was my fault.

Another great thing about gl is that it tells you what remote that was pushed to. So if you have deployed something and can’t figure out why you don’t see the changes, then it’s easy to see if you forgot to push your changes. This has saved my sanity many time.

Merging and Pulling

gm is a magic little command that does a lot of heavy lifting:

  1. If you have unstaged changes it stashes them
  2. Does a non-fast forward merge with the given branch. If none is given, it uses master. Non-fast forward is important because it logs where the merge was.
  3. Pop the stash, if necessary
  4. Gives you some stats back

gup is similar, only it tries to pull from the tracking branch. It does the stash as well, if necessary. If all else fails, it will try a rebase to make the merge, which doesn’t happen often (and, to me, is a sign that something went seriously wrong).

So, there are some every day tricks that I use to make my work with git a lot easier.

About the Author

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Tracking Original URL Through Authentication
If you read my other post about refreshing AWS tokens, then you probably have a use case for keeping track of the original requested resource while the user goes through authentication so you can route […]
Using Spring Beans in a Kafka Streams ExceptionHandler
There are many things to know before diving into Kafka Streams. If you haven’t already, check out these 5 things as a starting point. Bullet 2 mentions designing for exceptions. Ironically, this seems to be […]