About the author

Steven Harmansteven harman :: makes sweet software with computers!

For recent posts and more about me, scroll to the bottom.

Sponsors

Subscribe

  • Subscribe to my feed. via RSS
  • Subscribe via email via email

git pull with automatic rebase

y-u-no-rebase
Uploaded with Skitch!

To rebase, or not to rebase… for me its not really a question. I generally prefer a clean, linear commit history. Why? Because merge bubbles make history confusing, noisy, break git bisect.

Don’t believe me? Check out the pretty log to the right. See all those merge bubbles in there? Eww!

The Why?

The workflow that caused those merges was as follows:

  1. git pull (to bring local up to date)
  2. hackity-hack
  3. git commit
  4. git pull
  5. git push

By default git pull will fetch any new commits from the remote, and then merge any local changes in, resulting in the merge bubbles.

A better approach

I typically use the same workflow as above with one tweak. Rather than git pull, I use git pull --rebase, which will fetch the remote commits, and rebase your commits on top of the new commits from the remote. This is the “re-writing” of history folks often talk about.

Make it better, automatically!

You can tell git to use rebase, rather than merge, in one of two ways, depending on your situation.

To have all future branches automatically use rebase

$ git config branch.autosetuprebase always

You can add the --global switch to have all future branches, in all repositories on this machine, behave this way.

If, however, you have an existing branch, you’ll need

$ git config branch.branch-name.rebase true

Get more info

Be sure to check out the git man pages for more info on what those options mean and when you may or may not want to use them.

You might also want to check out my Git Workflows repository on The GitHubs where you can find a Keynote presentation (or PDF in the Downloads) explaining git rebase vs. git merge. Complete with pictures!

Technorati Tags: ,

What others are saying.

# re: git pull with automatic rebase
Gravatar Pat Gannon
Jun 17, 2011
Good stuff, Steve! Very helpful.
# re: git pull with automatic rebase
Gravatar Mark
Aug 15, 2011
I used `git config branch.autosetuprebase always` and
`git config branch.master.rebase true`.

However, when I do a `git pull origin master` and refresh the view in gitk, master updates but remotes/origin/master does not!

I have to use `git fetch` before they will agree.

?!
# re: git pull with automatic rebase
Gravatar Steven Harman
Aug 16, 2011
@Mark,
Two things:

1) In gitk there is a difference between "update" and "reload," with the latter actually reloading all data as if you'd just started the program. You might want to make sure you're reloading (via Cmd-F5).

2) As long as your local master branch is set to track the origin/master, you shouldn't need to specify it in your `git pull origin` command. Unless you ONLY want to merge/rebase changes on that branch and not others. Actually, you can optionally omit the "origin" argument as well (assuming git knows that origin is where you want to fetch from by default).
# re: git pull with automatic rebase
Gravatar Mark
Aug 16, 2011
@Steven
'git config push.default tracking' and
'git branch --set-upstream master origin/master'

seem to have fixed whatever was wrong.

Thanks!
Comments have been closed on this topic.