Git暂存保持索引冲突

4
我需要创建 pre-commit hook 来执行一些“健全性测试” (主要检查是否可以顺利部署)。 因为我只需要测试即将提交的更改,所以在运行测试之前我需要隐藏非暂存(non-staged)更改并在完成后还原它们。但是下面的情况可能会导致隐藏和索引之间发生冲突: 假设我有一个名为 testfile 的文件,其内容如下:
first line
second line
third line

现在我会在第二行和第三行之间添加一行

first line
second line
second and a half line
third line

然后运行 git add 命令将其加入暂存区。

现在我会在上一行的后面添加下一行,使文件看起来像这样:

first line
second line
second and a half line
second and three quarters line
third line

现在第一和第二行已经提交,第二行的一半被暂存,第二行的四分之三未暂存,第三行已提交。

让我们直接进入主题。现在当我运行 git stash --keep-index,然后执行我的测试,并尝试运行 git stash pop,结果如下:

Auto-merging testfile
CONFLICT (content): Merge conflict in testfile

文件内容如下:

first line
second line
second and a half line
<<<<<<< Updated upstream
=======
second and three quarters line
>>>>>>> Stashed changes
third line

我不知道如何重新应用存储的更改并在运行测试后避免这种冲突。如果您能指点我正确的方向,我将不胜感激。

1个回答

0

你基本上想要使用 git checkout --theirs 命令(对于冲突的文件),如果你想要保留储藏版本(--theirs 指的是储藏版本)。

然后使用 git reset HEAD 命令来从索引中删除更改(因为冲突而添加到索引中的更改),最后使用 git stash drop 命令。

如果有一个选项可以更好地处理 git-stash pop,那就太好了,因为这通常是你想要的。


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