简体   繁体   中英

git merge origin/branch vs. merge branch locally

什么是当我合并的不同origin/branchXbranchY和合并branchXbranchY

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM