如何使用 git commit -m --amend 将最新更改添加到非最新的提交?

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

我在 git 中进行了 2 次提交,即“版本 1”和“版本 2”。之后我对代码进行了另一次更改并使用了 git commit -m "version 1" --amend。我认为它将向版本 1 添加新的更改,但它将版本 2 提交消息的名称从“版本 2”更改为“版本 1”,并向其添加了新的更改。所以,现在我有两个版本 1 提交。但我想在原始版本 1 中添加新的更改。我该怎么做以及如何撤消此 --amend?请解释一下。

before changes

git log

commit hash
   version 2
commit hash
   version 1

after changes

git commit -m "version 1" --amend

git log

commit hash
   version 1 (This is where the new changes happened)
commit hash
   version 1 (I want it to go here)
bash git github version-control git-commit
1个回答
0
投票

一般不建议重写 git 历史记录。但是您可以使用交互式 rebase 工具来实现此目的。 更多信息

对于初学者来说,这是一个非常复杂的过程,但可以使用git-fork等工具变得更容易。

顺便说一句,你必须再次使用 amend 来撤消 amend 的更改。

© www.soinside.com 2019 - 2024. All rights reserved.