我有两个分支,即master和开发在一个GitHub库。我正在做我所有的开发在开发分支显示。
git branch development
git add *
git commit -m "My initial commit message"
git push -u origin development
现在我想将开发分支上的所有更改合并到主分支中。我目前的方法是:
git checkout master
git merge development
git push -u origin master
请让我知道我所遵循的程序是否正确。
步骤1
创建并切换到一个新的“dev”分支,其中您的本地git文件与远程同步,但“dev”分支还不存在。
git branch dev # create
git checkout dev # switch
# No need to git add or git commit, the current
# branch's files will be cloned to the new branch by-default.
git push --set-upstream origin dev # push the "dev" branch to the remote.
步骤2
对“dev”分支(如果您遵循第1步,则是当前的)进行更改,提交并将其推送到远程的“dev”分支。
git add .
git commit -S -m "my first commit to the dev branch" # remove the -S if you're not "secure", secure = when you already setup crypto private and public keys (i.e "verified" green sign in github)
git push -u origin dev # push the changes to the remote, -u origin dev is optional but good to use.
步骤3
将“开发”分支合并到“主”分支中。
git checkout dev # switch to "dev" branch if you're not already.
git merge master # optionally, this command is being used to resolve any conflicts if you pushed any changes to your "master" but "dev" doesn't have that commit.
git checkout master # switch to "master", which is the branch you want to be merged.
git merge --no-ff dev # merge the "dev" branch into the "master" one.
步骤1
创建并切换到一个新的“dev”分支,其中您的本地git文件与远程同步,但“dev”分支还不存在。
git branch dev # create
git checkout dev # switch
# No need to git add or git commit, the current
# branch's files will be cloned to the new branch by-default.
git push --set-upstream origin dev # push the "dev" branch to the remote.
步骤2
对“dev”分支(如果您遵循第1步,则是当前的)进行更改,提交并将其推送到远程的“dev”分支。
git add .
git commit -S -m "my first commit to the dev branch" # remove the -S if you're not "secure", secure = when you already setup crypto private and public keys (i.e "verified" green sign in github)
git push -u origin dev # push the changes to the remote, -u origin dev is optional but good to use.
步骤3
将“开发”分支合并到“主”分支中。
git checkout dev # switch to "dev" branch if you're not already.
git merge master # optionally, this command is being used to resolve any conflicts if you pushed any changes to your "master" but "dev" doesn't have that commit.
git checkout master # switch to "master", which is the branch you want to be merged.
git merge --no-ff dev # merge the "dev" branch into the "master" one.
如果您正在使用gerrit,那么以下命令可以完美地工作。
git checkout master
git merge --no-ff development
您可以使用默认的提交消息进行保存。确保已经生成了变更id。您可以使用下面的命令来确定。
git commit --amend
然后用下面的命令进行推送。
git push origin HEAD:refs/for/refs/heads/master
您可能会遇到如下所示的错误消息。
! [remote rejected] HEAD -> refs/for/refs/heads/master (you are not allowed to upload merges)
为了解决这个问题,gerrit项目管理员必须在gerrit中创建另一个引用,命名为'refs/for/refs/heads/master'或'refs/for/refs/heads/*'(这将覆盖所有
未来的分支)。然后授予该引用的“Push Merge Commit”权限,如果需要提交GCR,则授予“Submit”权限。
现在,再次尝试上面的push命令,它应该可以工作。
学分:
https://github.com/ReviewAssistant/reviewassistant/wiki/Merging-branches-in-Gerrit
https://stackoverflow.com/a/21199818/3877642
我通常喜欢先将master合并到开发中,这样如果有任何冲突,我可以在开发分支本身解决,并且master保持干净。
(on branch development)$ git merge master
(resolve any merge conflicts if there are any)
git checkout master
git merge development (there won't be any conflicts now)
这两种方法没有太大的区别,但我注意到,有时在合并它们之后,我还不想将分支合并到master中,或者在合并它们之前还有更多的工作要做,所以我倾向于让master保持不变,直到完成最后的工作。
编辑:来自评论
如果你想跟踪谁在何时进行了合并,你可以在合并时使用——no-ff标志。这通常只在将开发合并到主开发(最后一步)时有用,因为您可能需要在工作流中多次将主开发合并到开发(第一步),并且为这些创建提交节点可能不是很有用。
git merge --no-ff development