24

I can pull changes using git pull, but it merges my local commits. Is there a git rebase equivalent with which I can pull in remote changes?

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
michael
  • 106,540
  • 116
  • 246
  • 346
  • 1
    Related: [How to make Git pull use rebase by default for all clones?](http://stackoverflow.com/q/13846300/456814). –  May 23 '14 at 19:05

4 Answers4

33

Yes you can git pull --rebase.

You can also set that to be the default pull behaviour when you track a branch with git config branch.autosetuprebase always. Replace "always" with "remote" or "local" if you want to do it to those specific types of branches that you're tracking.

Now all you have to do is git pull.

If for some reason you want to do a merge, you can do git pull --no-rebase.

Hope this helps.

UPDATE: see comments below for how to do this on existing branches.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • 8
    `branch.autosetuprebase` only changes the default pull “mode” for *new* branches that have an upstream to track. `branch..rebase` is the variable that controls the operation for an individual branch (it is set to true for new “tracking” branches if `branch.autosetuprebase` is true when the branch is created). Use `git config branch..rebase true` to set it manually for a single branch. – Chris Johnsen Mar 15 '11 at 03:26
  • correct. I forgot to mention the existing tracking branches. You can also edit the config by hand if you know what you're doing. – Adam Dymitruk Mar 15 '11 at 03:47
  • 1
    To do it be editting the config file, find the section for the branch and add: rebase = true – phorgan1 Sep 27 '12 at 21:08
  • Also add a --global flag to apply it to the global git config and not just the repository: git config --global branch.autosetuprebase always – codingFoo Oct 30 '12 at 15:33
  • 3
    you can also set "rebase = True" in the [pull] section of your .gitconfig. – dbn Jun 20 '13 at 19:10
  • @dbw that's useful if you are already tracking branches and they are merging. – Adam Dymitruk Jun 20 '13 at 22:21
  • `git config branch.autosetuprebase always` will only rebase while updating the local tracking branch when doing `git pull` and otherwise do normal merges when merging from one branch to other. Is that correct understanding? – IsmailS May 28 '14 at 04:32
  • Also `git config --global pull.rebase true` – pratikpc Dec 16 '20 at 03:28
16

Instead of autosetuprebase, you can use the pull.rebase config option to change the behavior for every git pull (instead of only newly-created branches):

[pull]
    rebase = true

The difference is this will apply to non-tracking branches and any branches you had set up before enabling autosetuprebase. So if you really want pull --rebase to always be the default, pull.rebase is the way to go!

Justin M. Keyes
  • 6,679
  • 3
  • 33
  • 60
Jay Paroline
  • 2,487
  • 1
  • 22
  • 27
6

I usually use a fetch/rebase combination so my current (local) work stays at the top:

git fetch
git rebase origin/develop
karlphillip
  • 92,053
  • 36
  • 243
  • 426
2

To change default behavior from merge to rebase In git >= 1.7.9:

git config --global pull.rebase true

remove global if you want to apply for current repo only

Ali
  • 6,808
  • 3
  • 37
  • 47