Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge after fetch - how, exactly?

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?

like image 571
Major Productions Avatar asked Jan 25 '13 00:01

Major Productions


People also ask

Does git fetch merge changes?

git fetch is similar to pull but doesn't merge. i.e. it fetches remote updates ( refs and objects ) but your local stays the same (i.e. origin/master gets updated but master stays the same) . git pull pulls down from a remote and instantly merges.

How do you combine after pulling?

The git pull command is actually a combination of two other commands, git fetch followed by git merge . In the first stage of operation git pull will execute a git fetch scoped to the local branch that HEAD is pointed at. Once the content is downloaded, git pull will enter a merge workflow.

Do you need to merge after pull?

In a pull request, you propose that changes you've made on a head branch should be merged into a base branch. By default, any pull request can be merged at any time, unless the head branch is in conflict with the base branch.

What does git do when you do git fetch followed by git merge?

git fetch downloads all the changes needed to represent the given remote branch. Typically this is origin/master or similar. git merge merges two branches together by creating new commits or fast-forwarding (or a combination).


3 Answers

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'.

like image 173
Carl Norum Avatar answered Oct 23 '22 02:10

Carl Norum


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.

like image 40
John Szakmeister Avatar answered Oct 23 '22 02:10

John Szakmeister


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.

like image 8
maxschlepzig Avatar answered Oct 23 '22 02:10

maxschlepzig