Using Rebase instead of Merge for pulls

by @im_a_muppet on October 19, 2010

Instead of doing a regular pull

     git pull => git fetch && git merge remotes/origin/BRANCH BRANCH

To avoid the all the merge commit messages you can run the following command instead

     git pull --rebase => git fetch && git rebase remotes/origin/BRANCH BRANCH

I actually have the following in my ~/.gitconfig file

    [alias]
        up = !sh -c 'git pull --rebase --prune && git log --pretty=format:\"%Cred%ae %Creset- %C(yellow)%s %Creset(%ar)\" HEAD@{1}..'

So when I run ‘git up’ it does a pull and prints out a concisely what was pulled. I got it from Kyle Neath’s gist

Difference between rebase and a merge

Simple answer

git pull
  does a merge of your master and the remotes/origin/master and creates
  a commit to capture the merge.
git pull --rebase 
  stashes your unpushed commits from master, fast-forwards the upstream 
  changes from remotes/origin/master and then replays your stashed 
  commits.

Verbose answer

GIT PULL (merge)

Assume the following history exists and the current branch is "master":

           A---B---C master on origin
          /
     D---E---F---G master
 Then "git pull" will fetch and replay the changes from the remote
 master branch since it diverged from the local master (i.e., E) until
 its current commit (C) on top of master and record the result in a new
 commit along with the names of the two parent commits and a log message
 from the user describing the changes.

           A---B---C remotes/origin/master
          /         \
     D---E---F---G---H master

GIT PULL (rebase)

Assume the following history exists and the current branch is "topic":

             A---B---C master
            /
       D---E---F---G master on origin

   Then "git pull --rebase" will fetch, stash local commits (A, B, C), 
   fast-forward, and replay
   your stashed commits on top of master (as A', B', C') including 
   recreating the commit messages.

                     A'--B'--C' master
                    /
       D---E---F---G master on origin
blog comments powered by Disqus
Tweet