为什么 git merge 有时会删除不应该删除的更改?

发布于 2024-10-13 03:09:59 字数 496 浏览 3 评论 0原文

我经常在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三寸金莲 2024-10-20 03:09:59

因为 test 分支上的提交是最后进行的,并且 test 具有可以解析公共祖先提交的提交,所以默认行为是使用 test 中的新信息作为最新信息。您可以使用 -s 选项强制执行该行为。请参阅此链接以获取示例: http://www.kernel .org/pub/software/scm/git/docs/git-merge.html

已编辑工作流程示例

mkdir showoff_git
cd showoff_git
git init
touch file_a
echo "line 1" >> file_a
git add .
git commit -m "initial commit"
git checkout -b test
sed -i='' s/1/2/ file_a
git add .
git commit -m "bluffing"
git checkout master
git merge -s ours test

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

mkdir showoff_git
cd showoff_git
git init
touch file_a
echo "line 1" >> file_a
git add .
git commit -m "initial commit"
git checkout -b test
sed -i='' s/1/2/ file_a
git add .
git commit -m "bluffing"
git checkout master
git merge -s ours test
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文