Awesome Git Configuration, Shortcuts, and Aliases

Introduction

When working with Git, and software in general, the most important thing for me is that the act of using the application blends seamlessly into my workflow. It should feel natural, simple, and shouldn't slow you down.

I've worked with Git for a few years now, and like all things, I've kept track of my favorite tweaks and configurations. I'd like to share a few of the most awesome Git shortcuts I use on a daily basis.

Configuration

First, let's talk about a few configuration options that every engineer should be familiar with.

A Damn Good Difftool

When invoking Git's difftool and mergetool, instead of relying on Git's (admittedly simple and awesome) to help you resolve conflicts, I recommend the brilliant Kaleidoscope OS X App.

This app is incredibly useful for comparing differences in files, be it text, image, or even entire directories.

rerere

To enable rerere:

git config --global rerere.enabled 1

rerere, short for reuse recorded resolution, is a lesser-known feature of Git which allows a user to replay already-resolved merges.

This is an extremely nice feature that will automatically re-resolve any merge conflicts that you resolved in the past when using rebase or merge(with your freshly installed Kaleidoscope mergetool).

As an aside, if you incorrectly resolve a conflict, you can always use git rerere forget <pathspec> to force Git to forget!

autosetuprebase

To enable autosetuprebase:

git config --global branch.autosetuprebase always

The autosetuprebase option will tell Git to automatically rebase instead of merge in new changes when pulling. Settings this configuration option to always has a subtle difference from the default setting of true. always will not only change the behavior on remote branches, but on local branches as well.

I prefer this setting to the default, as it keeps all commit history instead of merging in new changes as a single merge commit. This fits inline with my general strategy of keeping commit history very succinct and clean. I'm a big fan of fixup and squash commits before bringing new features over to a develop or master branch.

It's important to note that any existing branches will retain their current configuration on a branch-by-branch basis.

autosetupmerge

To enable autosetupmerge:

git config --global branch.autosetupmerge always

Often mistaken as the companion setting to autosetuprebase, autosetupmerge is quite different.

autosetupmerge specifies whether git branch and git checkout -b should pass the --track flag by default. Setting this to always causes both remote and local branches to automatically create new branches which track their parent.

Shortcuts/Aliases

current-branch or cb

To enable git current-branch:

git config --global alias.current-branch 'rev-parse --symbolic-full-name --abbrev-ref HEAD'

This one is super simple. It simply outputs the currently checked out branch. We'll be using it again in a second...

shove

git config --global alias.shove '!f() { H=$(git current-branch) && git push -u origin $H; }; f'

A super useful alias, git shove is generally my first command fired off after creating a new branch.

What shove does is perform a git push -u origin <current_branch>. You can think of it as a nice first step to tell your origin that you've created a new local branch you want to track remotely as well.

lg

git lg is a brilliant configuration to output the git log command in a readable, color-coded format that shows a GUI representation of your branches. Not only does it show where branches begin and end, it shows the commits they contain, and how they replayed onto other branches! Super handy.

git cleanup

git cleanup deletes any branches in your local that have been merged into the master or develop branches. This is handy to run from time to time to purge unused local branches.

cleanup = "!git branch --merged | grep  -v '\\*\\|master\\|develop' | xargs -n 1 git branch -d"

Hub

If you use Github, hub is the perfect addition. hub is a command line utility that allows awesome interaction with Github, like creating pull requests, forking, and interacting with remote branches almost as if they were local.

For a pro-tip, alias hub to the git command:

eval "$(hub alias -s)"

I hope these tips are helpful -- if you learned anything, or have any comments, please post below! I'm happy to add more or follow up.


← Back to Overview