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
Android Development for iOS Developers
Android development has greatly improved since the early days. Maybe you tried it out when Android development was done in Eclipse, emulators were slow and buggy, and Java was the required language. Things have changed […]
Add a custom object to your Liquibase diff
Adding a custom object to your liquibase diff is a pretty simple two step process. Create an implementation of DatabaseObject Create an implementation of SnapshotGenerator In my case I wanted to add tracking of Stored […]
Keeping Secrets Out of Terraform State
There are many instances where you will want to create resources via Terraform with secrets that you just don’t want anyone to see. These could be IAM credentials, certificates, RDS DB credentials, etc. One problem […]
Validating Terraform Plans using Open Policy Agent
When developing infrastructure as code using terraform, it can be difficult to test and validate changes without executing the code against a real environment. The feedback loop between writing a line of code and understanding […]