How to merge remote master to local branch

GitBranchPull

Git Problem Overview


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?)

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

I found out it was:

$ git fetch upstream
$ git merge upstream/master

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartynView Question on Stackoverflow
Solution 1 - GitJoey AdamsView Answer on Stackoverflow
Solution 2 - GitMartynView Answer on Stackoverflow
Solution 3 - GitSergiy SeletskyyView Answer on Stackoverflow
Solution 4 - GitAJCView Answer on Stackoverflow
Solution 5 - GitTarzanView Answer on Stackoverflow