Git merge after fetch - how, exactly?

31,573

Solution 1

git merge origin/master should work. Since master is usually a tracking branch, you could also do git pull from that branch and it will do a fetch & merge for you.

If you have local changes on your master that aren't reflected on origin, you might want git rebase origin/master to make sure your commits are 'on top'.

Solution 2

I typically do this:

git merge --ff-only @{u}

Which says, "only do a fast-forward merge from the upstream tracking branch." It's nice because if it fails, then I know I introduced something on master that is not upstream. I have that aliased to ff, just to make it easier to type.

If there are changes, and you simply want to merge them, you can do:

git merge @{u}

Which will merge in the upstream branch. However, if you'd like a cleaner history (and avoid the "Merging 'origin/master' into 'master'" commits, then you might want to consider rebasing instead:

git rebase @{u}

Of course, you can you origin/master instead of @{u} in any of these examples.

Solution 3

The command

git pull $some_url

is equivalent to

git fetch $some_url
git merge FETCH_HEAD

See also the git-pull(1) man page for details, especially the first two paragraphs of the description.

Share:
31,573
Major Productions
Author by

Major Productions

This space for rent

Updated on July 09, 2022

Comments

  • Major Productions
    Major Productions almost 2 years

    I've read from various sources that it's usually a better idea to fetch then merge rather than simply pull as it allows for finer control. That said, I've yet to find actually how to do it. Case in point:

    There was a small change made to some of the code in one of my GitHub repository's master branch. I was able to fetch it, but I don't know how to actually merge the differences in with my local master branch. git branch lists all of the local branches I have, but nothing indicating anything to merge to.

    So, is it just something like git merge master or git merge origin/master? What am I missing?

  • IsmailS
    IsmailS over 10 years
    Do I need to git rebase origin/master before git merge orgin/master? I'm asking only for the case when I have local changes on master that aren't reflected on origin.
  • Carl Norum
    Carl Norum over 10 years
    You need to do one or the other, not both. Which to choose depends on the outcome you're after, but rebase is usually what you want.