Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git merge origin/branch vs. merge branch locally

Tags:

git

What is the different between when I merge the origin/branchX into branchY and merge branchX into branchY?

like image 695
user1167753 Avatar asked Mar 29 '16 10:03

user1167753


People also ask

What does git merge origin branch do?

Merging is Git's way of putting a forked history back together again. The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.

Can I merge branches locally?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.

What is the difference between fast forward merge and 3 way merge of branches?

It is called so because, in the 3-way merge, Git uses three commits to generate the merge commit; two branch tips and their common ancestor. Typically, the fast forward merge is used by developers for small features or bug fixes while the 3-way merge is reserved for integrating longer-running features.

What is the difference between git merge and git pull?

The git pull command first runs git fetch which downloads content from the specified remote repository. Then a git merge is executed to merge the remote content refs and heads into a new local merge commit.


2 Answers

origin/branchX is a remote tracking branch, and gets updated with changes from the remote repository every time you do a git fetch. On the other hand, branchX is your local version of this branch. branchX may be out of sync with origin/branchX which in turn may be out of sync with what is actually on the remote repository.

Hence the difference in doing a merge will depend on the differences in the various incarnates of branchX. If you want to merge the very latest branchX into your branchY then you should do the following:

git fetch origin          # update remote tracking branchX
git checkout branchY      # switch to branchY
git merge origin/branchX  # merge

If you want to also update your local branchX in the process, you could do this:

git checkout branchX
git pull origin branchX
git checkout branchY
git merge branchX

However, you might have the need to merge your local copy of branchX into branchY without synching either branch with the remote. This would be a typical use case if, for example, new changes came into branchX on the remote and you did not want to bring them into branchY just yet. In this case you would perform the merge like this:

git checkout branchY
git merge branchX
like image 131
Tim Biegeleisen Avatar answered Sep 17 '22 07:09

Tim Biegeleisen


When merging the remote branch - you are retreiving it with all remote changes that have been applied to it by other devs, but that might not yet be on your local branch (though without your local changes that have not yet been pushed to remote branch)

And when merging local branch you are merging it with all changes that you have done locally that have not yet been pushed to remote branch, but without all remote changes that have been applied to it by other devs, but that might not yet be on your local branch.

         -*-*-* branchX 
*-*-*-*-/-*-*-*-*-* Origin/branchX 

Imagine these are your local and remote branches, you have commited changes twice to your local branch and somebody has added 5 commits and pushed to repo, so you don't have those 5 on your local branch yet.

like image 22
naneri Avatar answered Sep 20 '22 07:09

naneri