我在 git 中有如下历史记录:
<hash2> commit2
<hash1> commit1
<hash0> initial commit
我执行
git rebase -i <hash0>
并选择编辑 (pick -> e
) 两个提交(commit1
和 commit2
)。编辑 commit1
后,我尝试通过执行 git commit --amend
来提交更改,我编辑提交消息并保存它,git
说“确定”。
然后我做了
git rebase --continue
,现在git
正在尝试将commit2
合并到commit1
中,它说应用commit2
后存在一些冲突,所以我编辑这个提交并保存它,但是当我做 git commit --amend
而不是像 commit2
那样编写更改,它将它们合并到 commit1
。
也许我错过了一些东西,为什么会发生这种情况以及如何解决这个问题(我希望我的 3 次提交历史记录在变基后保持相同)?
当您进行交互式变基并选择编辑提交时,您需要处理每个提交权限以保持历史记录相同。首先,使用
git rebase -i <hash0>
启动变基,并将 commit1 和 commit2 的 pick
更改为 edit
。当它停止在 commit1 时,进行更改,然后使用 git commit --amend
和 git rebase --continue
。然后它在 commit2 处停止并显示冲突。解决冲突,然后使用 git add <resolved-files>
来上演它们。不用git commit --amend
,只需使用git commit
单独保存commit2,然后用git rebase --continue
继续rebase。这样,每个提交都保持独立,并且历史记录在变基后看起来是相同的。