git revert merge 策略问题(他们的)

标签 git git-merge git-revert

我想恢复到该分支的上一个提交,并获取该提交中的所有更改,而不是当前提交中的任何更改。

我已经设置了我的测试目录如下:

mkdir test_dir
cd test_dir

git init .

touch a_file

git add a_file
git commit -am "file added."
# say commit id 0_afile

echo "1 line" >> a_file
git commit -am "1 line"
# say commit id 1_afile

echo "2 line" >> a_file
git commit -am "2 line"
# say commit id 2_afile

echo "3 line" >> a_file
git commit -am "3 line"
# say commit id 3_afile

echo "4 line" >> a_file
git commit -am "4 line"
# say commit id 4_afile

当我运行git revert --no-edit -X theirs 2_afile时。我期望提交 0_afile1_afile2_afile 的内容,但结果不包含 2_afile 的内容。有人可以指出我做错了什么吗?

最佳答案

这对我来说似乎有些自相矛盾:

I want to revert to a previous commit of the branch ...

(粗体是我的;请注意,这很简单;请参阅,例如 Reset or revert a specific file to a specific revision using Git?,尽管它适用于某些特定文件)

... with taking all the changes in that commit and nothing from the current one.

提交不是更改,提交是状态

举个简单的例子,假设我告诉您,周五外面的温度目前为 68°F (20°C)。这就是一个状态。如果我问你与昨天的气温相比有什么变化,你能告诉我吗?您还需要首先了解什么?

如果温度上升 9°F (5°C),那就是变化。如果我告诉您周一与周日的气温上升了那么多,那么周日的气温是多少?我还需要先告诉你什么?


在 Git 中,每次提交都是所有文件的完整快照,无论您提交时文件处于什么状态。要从提交中获取更改,有必要将该提交与其他提交进行比较。明显的“其他”提交是紧接其前的提交,即该提交的。如果一个提交只有一个父提交,Git 可以自动执行此操作:它提取前一个提交的文件(一种状态)和给定提交的文件(另一种状态),然后减去它们以生成集合变化。

如果您想要先前提交的一个文件的内容,那很简单;请参阅链接的问题,这是重复的。 git revert 的作用完全不同:它将提交(一种状态)转变为更改,然后尝试将相同的更改应用于当前状态。请参阅我最近对 ​​Revert only a single file of a pushed commit 的回答,例如。

关于git revert merge 策略问题(他们的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53566628/

相关文章:

git - 解决 git 冲突 : git mergetool doesn't work

git - 如何像 git-add -p 那样仅部分恢复 git?

git - 恢复不在当前分支中的 git 提交

ios - 如何为 arm64 位支持 iOS 构建 libssh2

git - gitcherry-pick 中跳过的文件会包含在以后的 git merge 中吗?

git - 如何 merge 两个本地存储库

git - 在 git 或 tortoisegit 中撤消还原

git - 如何在一个 git 仓库中管理两个 TFS 项目?

git - 使用 Github : fatal: could not read Username 推送提交时出错

iphone - 在 iPhone 项目中使用 git