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
An Exploration in Rust: Musings From a Java/C++ Developer
Why Rust? It’s fast (runtime performance) It’s small (binary size) It’s safe (no memory leaks) It’s modern (build system, language features, etc) When Is It Worth It? Embedded systems (where it is implied that interpreted […]
Getting Started with CSS Container Queries
For as long as I’ve been working full-time on the front-end, I’ve heard about the promise of container queries and their potential to solve the majority of our responsive web design needs. And, for as […]
Simple improvements to making decisions in teams
Software development teams need to make a lot of decisions. Functional requirements, non-functional requirements, user experience, API contracts, tech stack, architecture, database schemas, cloud providers, deployment strategy, test strategy, security, and the list goes on. […]
JavaScript Bundle Optimization – Polyfills
If you are lucky enough to only support a small subset of browsers (for example, you are targeting a controlled set of users), feel free to move along. However, if your website is open to […]