为什么 git merge 有时会删除不应该删除的更改?
我经常在 git 中遇到一些奇怪的行为。当我合并到另一个分支(其中对同一文件进行了不相关的更改)时,我对一个分支中的文件所做的更改将被删除。
假设我从分支大师开始。以下是所发生情况的粗略概述:
vim foo.txt
git add foo.txt
git commit
git checkout -b test
vim foo.txt
git commit -a -m added a new line to foo.txt
git checkout master
vim foo.txt
git commit -a -m made some unrelated change
git merge test
此时,我会发现我在 master 分支中的 foo.txt 中所做的更改已被删除。
在这一切过程中,我正在进行许多其他更改并执行其他 git 操作。由于像这样的合并是 git 的全部要点,所以我觉得我可能在某些时候做错了什么。
有人知道是什么吗?
Every so often, I've been experiencing some strange behavior in git. where changes I make to a file in one branch are removed when I merge in another branch in which unrelated changes have been made to the same file.
Let's say I start in branch master. Here's the rough outline of what happens:
vim foo.txt
git add foo.txt
git commit
git checkout -b test
vim foo.txt
git commit -a -m added a new line to foo.txt
git checkout master
vim foo.txt
git commit -a -m made some unrelated change
git merge test
At this point, I will discover that the change I made in foo.txt in the master branch has been removed.
I am making many other changes and performing other git operations in the middle of all of this. Since merges like this are the entire point of git, I feel like I am probably doing something wrong, at some point.
Does anyone have any idea what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为 test 分支上的提交是最后进行的,并且 test 具有可以解析公共祖先提交的提交,所以默认行为是使用 test 中的新信息作为最新信息。您可以使用 -s 选项强制执行该行为。请参阅此链接以获取示例: http://www.kernel .org/pub/software/scm/git/docs/git-merge.html
已编辑工作流程示例
because the commit on the test branch was made last and test has a commit that can resolve a common ancestor commit, then the default behaviour is to use the new information from test as the most up-to-date information. you can force the behaviour by using the -s option. See this link for examples: http://www.kernel.org/pub/software/scm/git/docs/git-merge.html
EDITED with workflow example