Git:恢复合并提交:已经是最新的

问题描述 投票:0回答:0

我对 git revert 和这里的提交历史缺乏一些基本的了解。 可重现的例子:

mkdir git_test
cd git_test
git init
touch test.txt
echo "asdf" >> test.txt
git add test.txt
git commit -m "added file"
echo "asdf2" >> test.txt
git add -u
git commit -m "added another line in main"
git checkout -b feature
echo "asdf3 in feature" >> test.txt
git add -u
git commit -m "added a third line in feature"
echo "asdf4 in feature" >> test.txt
git add -u
git commit -m "added 4th line in feature"
git checkout master
echo "adding line 3 in main" >> test.txt
git add -u
git commit -m "added line 3 in main"

我有两个分支,

feature
master
(我在上面称之为main,抱歉)! 现在我可以将 feature 合并到 main 中,并解决冲突:

git merge feature # accept incoming changes in conflict
git add -u
git commit -m "merge feature into main"

如果我后悔这次合并我可以做

git revert -m 1 be62f73
。由于这是在模仿公共分支,所以我不想做
git reset
.

所以,这没问题,但通常我不能将任何东西直接合并到

master
中,所以我必须创建一个
PR
,我必须先将
master
合并到
feature
中:

git checkout feature
git merge master # accept currenct changes in conflict
git add -u
git commit -m "merge master into feature"
git checkout master
git merge feature

如果我现在尝试

git revert -m 1 225747f
我得到
Already up to date! On branch master \n nothing to commit, working tree clean
.

那么,为什么我不能在第二个实例中恢复它?一定有一些我不理解的基本概念吗? 如果

HEAD
是合并提交,那么
HEAD
真正包含什么?还原合并提交是什么意思?当我合并两个分支时,合并提交是否只是表示“这里发生了一些合并”,但是来自另一个分支的提交是否混入了提交历史? 如果我对将
feature
合并到
master
中感到遗憾,是否有办法只对从 feature 移动的提交中的
parts
感到遗憾,而不是关心实际的合并提交?我真的不明白这个合并提交与合并时 incoming 到新分支的常规提交有何关系。

git merge revert
© www.soinside.com 2019 - 2024. All rights reserved.