Git: pulling

So you're working on a project, made some commits and is ready to push. You pull to make sure you're up to date but lo and behold, the branch has diverged and you'll have to merge - RIP.

Pulling in git is one of the first things we learn, so we never question it. But what if there was a better, easier way.

Rebasing

If your branch has diverged, your history would look like this:

      A---B---C
     /
D---E---F---G

By default, pulling uses merging to join the diverted branches which adds a separate commit and leave the history looking like this:

      A---B---C
     /         \
D---E---F---G---H

Ugly. Instead you can use the --rebase flag when pulling to put your commits directly after the previous ones, which looks like:

D---E---F---G---A---B---C

That's a lot better. To make this the default behaviour use:

$ git config --global pull.rebase true

Autostashing

So now you're happily pulling without the worry of merging, or of your history looking bad. But every time you do, if you have unstaged changes, git will complain. So, is there a way around that? Of course, it's called autostashing.

As the name suggests, before a pull, your unstaged changes will be stashed, git will then pull and finally pop the stash. All of this will be done automagically meaning there's less tedious work for you.

To enable it, use the --autostash flag. To do it by default use:

$ git config --global rebase.autostash true

Further reading