288

I have a local branch of a project ("configUpdate") that I've forked from somebody else's project and I've done a load of changes on it and would like to merge the changes they've made in to my local branch.

I've tried

git pull --rebase origin configUpdate

but it hasn't grabbed the latest changes - how can I merge the two? (also for bonus points what did I do with the pull --rebase command?)

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Martyn
  • 16,432
  • 24
  • 71
  • 104
  • Possible duplicate of [Git: Merge a Remote branch locally](https://stackoverflow.com/questions/21651185/git-merge-a-remote-branch-locally) – Michael Freidgeim May 30 '17 at 06:11

5 Answers5

425

From your feature branch (e.g configUpdate) run:

git fetch
git rebase origin/master

Or the shorter form:

git pull --rebase

Why this works:

  • git merge branchname takes new commits from the branch branchname, and adds them to the current branch. If necessary, it automatically adds a "Merge" commit on top.

  • git rebase branchname takes new commits from the branch branchname, and inserts them "under" your changes. More precisely, it modifies the history of the current branch such that it is based on the tip of branchname, with any changes you made on top of that.

  • git pull is basically the same as git fetch; git merge origin/master.

  • git pull --rebase is basically the same as git fetch; git rebase origin/master.

So why would you want to use git pull --rebase rather than git pull? Here's a simple example:

  • You start working on a new feature.

  • By the time you're ready to push your changes, several commits have been pushed by other developers.

  • If you git pull (which uses merge), your changes will be buried by the new commits, in addition to an automatically-created merge commit.

  • If you git pull --rebase instead, git will fast forward your master to upstream's, then apply your changes on top.

Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
  • I did this on the correct branch but I can still see differences between my local files and the remote master branch of the original project (even though it says everything is up to date!) maybe I've set the project up incorrectly? Any way to check? – Martyn Aug 26 '11 at 06:15
  • 1
    @Martyn: The differences should be your local changes. Make another fork of the remote branch and check if that one has the correct file content. – ZeissS Aug 26 '11 at 07:52
  • 7
    I know this is an old answer, but note that you may prefer to do a MERGE instead of a rebase. You don't want to do a rebase if you'll later have to merge to a remote repo that already has some of your changes. – Domino Mar 06 '17 at 21:51
  • 3
    How about (assuming you're currently on branch configUpdate) ...  git pull is basically the same as git fetch; git merge origin/master.  <-- NOT TRUE – Chris Oct 18 '17 at 17:42
  • @Chris: In what sense is it not true? The assumption of origin/master as the upstream? Or has git pull been changed? – Joey Adams Oct 25 '17 at 18:12
  • @Joey Adams, very nicely explained and I did git pull and like you said my changes *buried under other change*. should have done git pull --rebase. – yantaq Feb 15 '18 at 18:44
  • @JoeyAdams, I agree with chris that it is not true it would be better to say `git merge ` – Eelke Nov 21 '18 at 09:29
  • The first form works for me, the second does not. `git pull` does not equate to `git fetch; git merge origin/master` for me. – RCross Jul 01 '20 at 09:04
  • @RCross: Git now supports changing the default behavior of `git pull` to rebase instead of merge. Run `git config --get pull.rebase` to see what your default is. – Joey Adams Jul 01 '20 at 23:14
  • Btw `$git pull rebase` didn't work for me but `$git fetch $git rebase origin/master´ works fine – Ikbel Jan 22 '21 at 10:34
  • @Ikbelbenab Might be an issue with your syntax. You need two dashes: `git pull --rebase` . If it still doesn't work, do `git branch --set-upstream-to origin/master` so that `git pull --rebase` knows where to pull from. – Joey Adams Jan 22 '21 at 16:55
98

I found out it was:

$ git fetch upstream
$ git merge upstream/master
Martyn
  • 16,432
  • 24
  • 71
  • 104
47

Switch to your local branch

> git checkout configUpdate

Merge remote master to your branch

> git rebase master configUpdate

In case you have any conflicts, correct them and for each conflicted file do the command

> git add [path_to_file/conflicted_file] (e.g. git add app/assets/javascripts/test.js)

Continue rebase

> git rebase --continue

Sergiy Seletskyy
  • 16,236
  • 7
  • 69
  • 80
22

git rebase didn't seem to work for me. After git rebase, when I try to push changes to my local branch, I kept getting an error ("hint: Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes (e.g. 'git pull ...') before pushing again.") even after git pull. What finally worked for me was git merge.

git checkout <local_branch>
git merge <master> 

If you are a beginner like me, here is a good article on git merge vs git rebase. https://www.atlassian.com/git/tutorials/merging-vs-rebasing

AJC
  • 981
  • 10
  • 17
4

This applies to developers using Visual Studio.

  1. Click Git menu > Manage Branches > remotes/origin
  2. Right-click master > Merge 'origin/master' into [local branch]

Note: master is called main in recent git repositories.

enter image description here

Tarzan
  • 4,270
  • 8
  • 50
  • 70