情况:
从 A
的 Master 开始,我在 B
分支并进行了一些更改,然后将该分支 merge 回 (C)
。在进行了更多更改后,我在 D
但发现我需要部署代码,而无需在分支中发生更改。如果我不手动 merge 它就好了。
A_______C___D
\ /
\_B_/
首先,我想知道我应该从这里做什么来部署代码,就好像 merge 从未发生过一样。注意:在分支中编辑过的相同文件都没有在主控中编辑过。
其次...
我没有时间想出最好的方法来处理这个问题,所以我删除了分支添加的文件,并手动还原了分支中所做的一些编辑,然后提交了部署结果( F
)
A_______C___D___F
\ /
\_B_/
我希望能够继续开发分支并将 master 的任何更改 merge 到其中以使其保持最新,但如果我这样做,我创建 F
所做的事情将是 merge 并导致文件被删除并恢复编辑。处理此问题的最佳方法是什么?
最佳答案
您可以使用 rebase 一步完成:
git rebase --onto A C D
我刚刚对此进行了测试,并得到了适当的结果:
$ edit test.txt
$ git add .
$ git commit -mA
$ git checkout -b the_branch
$ edit test.txt
$ git commit -a -mB
$ git checkout master
$ git merge master the_branch --no-ff
$ edit test.txt
$ git commit -a -mD
从这里你有你描述的情况。 然后:
$ git rebase --onto <SHA1-for-A> <SHA1-for-C> master
rebases 从 C(排除)提交到 master,再到 A。 我需要解决一些冲突,因为我在 B 和 D 中的相同位置进行了修改,但我认为你不会。
_D'
/
/
A_______C___D
\ /
\_B_/
关于 git rebase --onto
的文档,这或多或少是你的情况:
http://git-scm.com/docs/git-rebase
如果你有:
A_______C___D___F
\ /
\_B_/
,那么你现在有:
_D'___F'_(master)
/
/
A_______C___D___F
\ /
\_B_/(the_branch)
从这里开始,将 master 中的更改 merge 到分支中很容易。完全丢弃提交 F'
。
$ git checkout master # if you were not here already
$ git branch old_fix # if you want to be able to return to F' later
$ git reset --hard <SHA1-to-D'>
在上面的命令之后你有:
(master)
/
_D'___F'_(old_fix)
/
/
A_______C___D___F
\ /
\_B_/(the_branch)
将 master 的更新 merge 到 the_branch 中:
$ git checkout the_branch
$ git merge master
...并解决冲突。
关于Git:如何将 "undo" merge ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4434787/