将我的本地分支与远程主服务器合并

时间:2017-12-07 07:28:07

标签: git merge

我是git的新手。我正在与团队一起开发代码库。 我的远程存储库的状态是:

git remote show origin                                # show the status
Remote branches:
....
                     master                             tracked
                     branch_to_be_merged                 tracked
...
Local branches configured for git pull:
master                                                 merges with remote master
branch_to_be_merged                                      merges with remote branch_to_be_merged      

Local refs configured for git push:
master                                                 pushes to master
branch_to_be_merged                                       pushes to  branch_to_be_merged       

我一直致力于branch_to_be_merged上的一项功能,而master则是积极开发的。 现在我需要向公众提供我的功能,我将使用以下一系列git命令进行合并。

以下是否正确?

git fetch origin                #make sure I am updated with latest remote changes
git checkout -b branch_to_be_merged   # the currently checked out branch is the one of interest
git branch                      # check I am on the branch I would like to merge later (it should now show *branch_to_be_merged)
git pull origin master           # apply the latest remote changes to my local branch_to_be_merged [I EXPECT CONFLICTS HERE]
git push branch_to_be_merged     # push the changes to remote
git checkout -b master           # the currently checked out branch is master
git branch                      # check I am on the branch I would like to merge later (it should now show *master)
git pull origin master           # apply the latest remote changes to my local master
git merge branch_to_be_merged    # merge  branch_to_be_merged to local master
git push master                 # publish the updates to master

2 个答案:

答案 0 :(得分:2)

您带评论的版本

# you don't need to put 'origin'. If you have more than one remote, you can use 'git fetch --all'
git fetch origin                #make sure I am updated with latest remote changes
# the '-b' creates a new branch, this is not possible since the branch already exists. Remove '-b'
git checkout -b branch_to_be_merged   # the currently checked out branch is the one of interest
git branch                      # check I am on the branch I would like to merge later (it should now show *branch_to_be_merged)
# I would advice against doing this. Usually, 'pull' is used to update a
# local branch of the same name as the remote branch (it is a shorthand for
# 'git fetch && git merge origin/branch_name'.
# Secondly, 'pull' can be configured to use either 'merge' or 'rebase'
# so the behavior of this command depends on some configuration
git pull origin master           # apply the latest remote changes to my local branch_to_be_merged [I EXPECT CONFLICTS HERE]
# if you are merging the branch back to master you will probably delete
# it afterwards. Hence, you don't need to push it. You'll push your
# commits when you push the updated master branch
git push branch_to_be_merged     # push the changes to remote
# Again, no '-b' for existing branches
git checkout -b master           # the currently checked out branch is master
git branch                      # check I am on the branch I would like to merge later (it should now show *master)
# Simply use git pull, as this will automatically pull in the changes
# from the correct remote branch (so you cannot accidentally pull changes
# from a different branch)
git pull origin master           # apply the latest remote changes to my local master
git merge branch_to_be_merged    # merge  branch_to_be_merged to local master
git push master                 # publish the updates to master

简短解决方案

我会建议以下命令:

git checkout master
# You don't need a git fetch at all, since pull does a fetch
git pull
git merge branch_to_be_merged # merge conflicts here
git push
# optionally delete the merged branch and its remote
git branch -d branch_to_be_merged
git push origin :branch_to_be_merged

答案 1 :(得分:1)

签出master时不需要使用-b开关,因为分支已存在

相关问题