将开发分支与主分支合并

934

我在GitHub存储库中有两个分支,分别是masterdevelopment。如下所示,我在development分支中进行所有开发工作。

git branch development
git add *
git commit -m "My initial commit message"
git push -u origin development

现在我想将development分支上的所有更改合并到master分支上。我的当前方法是:

git checkout master 
git merge development
git push -u origin master 
请告诉我我正在遵循的步骤是否正确。

10
git pull -u 用于为分支设置上游追踪(如果推送多个分支,则为所有分支设置)。一旦设置了追踪,它将持久存在。没有必要反复使用它。 - David Culp
15个回答

3

1)在开发分支上,使用以下命令检查git状态:

git status

不应存在未提交的代码。如果有,请将您的代码推送到开发分支:

git add *

git commit -m "My initial commit message"

git push origin Development

2) 在开发分支上运行以下两个命令:

git branch -f master HEAD

git push -f origin master

它将把你的开发分支代码推送到主分支。

这是否会将所有开发提交推送到主分支,还是只是将一个新的单独提交添加到主分支? - rollsch
2
这个程序相关的内容该如何实际运作?特别是当你在 develop 分支时输入 "git branch master" 看起来非常混乱。如果已经有一个名为 master 的分支,那么怎么能够创建一个新的叫做 master 的分支呢?文档中指出 -f 命令会执行以下操作:将 <branchname> 重置到 <startpoint>。这是什么意思呢? - John Little
这不是在强制将本地主分支推送到远程主分支吗?如果你在团队中工作,这似乎是一个糟糕的想法。 - Nick
1
不建议使用“-f”。 - DawnSong

2
一旦您“checkout”开发分支,您就可以开始对代码进行修改并创建新的提交。
 git add .
 git commit -m "first commit"
 git push origin dev
 git merge master

 git checkout master 
 git merge dev
 git push origin master 

1
我认为最简单的解决方案是:

git checkout master
git remote update
git merge origin/Develop -X theirs
git commit -m commit -m "New release"
git push --recurse-submodules=check --progress "origin" refs/heads/Master

这也保留了所有正在使用的分支的历史记录。

0
如果您正在使用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”权限和必要的“Submit”权限以提交GCR。
现在,请再次尝试以上推送命令,应该可以正常工作。
致谢:

https://github.com/ReviewAssistant/reviewassistant/wiki/Merging-branches-in-Gerrit

https://dev59.com/eGEi5IYBdhLWcg3whckh#21199818


-7
1. //push the latest changes of current development branch if any        
git push (current development branch)

2. //switch to master branch
git checkout master 

3. //pull all the changes if any from (current development branch)
git pull origin (current development branch)

4. //Now merge development into master    
git merge development

5. //push the master branch
git push origin master

Error
To https://github.com/rajputankit22/todos-posts.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/rajputankit22/todos-posts.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Then Use 
5. //push the master branch forcefully
git push -f origin master

5
除非您非常确定知道本地分支缺少远程分支提交记录的原因,否则在看到错误时强制推送几乎永远不是正确的做法。通常,最好回到第3步再次执行pull操作。此外,与现有答案相比,不清楚这个答案添加了什么价值。 - Kyle Strand

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接