简体   繁体   English

“修改”先前的提交并编辑消息

[英]“amend” previous commit and edit the message

Let say that I want to change HEAD~2 by my current last commit "HEAD" without the change made by HEAD~. 假设我要通过当前的最后一次提交“ HEAD”来更改HEAD〜2,而无需HEAD〜进行更改。 Moreover, I'd like to edit the message from HEAD~2 and not overwrite it (just few changes should be done on the message). 此外,我想编辑来自HEAD〜2的消息,而不是覆盖它(应该对消息进行很少的更改)。 I found that way : this is my current git log : 我发现是这样的:这是我当前的git日志: 在此处输入图片说明

Then I did this following command : 然后我执行以下命令:

git reset --mixed Head~3
git add new_file.cpp
git commit -m "Erased previous new_file.cpp by current new_file"
git add other_file.cpp
git commit -m "Added other_file.cpp"

So now I get this : 所以现在我得到了:

在此处输入图片说明

As you can see, I have rewrite all my commit to make it clean. 如您所见,我已经重写了所有提交以使其变得干净。 And as I am quite newbie on reset... it can be a bit dirty. 而且由于我是新手,因此可能会有点脏。

Could it be possible to get this cleaner just by "editting" the commits ? 是否可以仅通过“编辑”提交来获得此清理程序? For instance I'd like to edit the previous message from the first commit and keep the message of the second intact. 例如,我想编辑第一次提交中的上一条消息,并保持第二条消息的原样。

Moreover, the way I did the modification let me think the previous commit are still present and must pollute the repository : will they be erased or stay there foreever ? 而且,我进行修改的方式让我认为以前的提交仍然存在,并且必须污染存储库:它们将被删除还是永远保留在此?

PS : the GitHub repository (the reflog is quite horrible as I made some command mistake before finding the right command) PS: GitHub存储库 (reflog非常可怕,因为我在找到正确的命令之前犯了一些命令错误)

There is no possibility to change commit message without changing (at least SHA) of all its descendants. 不更改(至少SHA)所有后代的提交消息是不可能的。 However if you still want to do so (for example you made typo earlier and you still haven't published your commit) then you can do: 但是,如果您仍然想这样做(例如,您较早输入错字,但您仍未发布提交),则可以执行以下操作:

git rebase --interactive HEAD~2

And then change pick at the beginning of the line with your commit to reword . 然后在行的开头更改pick ,并承诺reword It will do all git checkout / git commit --amend / git rebase magic for you. 它将为您完成所有git checkout / git commit --amend / git rebase魔术。

If you want to learn more: 如果您想了解更多信息:

Moreover, the way I did the modification let me think the previous commit are still present and must pollute the repository : will they be erased or stay there foreever ? 而且,我进行修改的方式让我认为以前的提交仍然存在,并且必须污染存储库:它们将被删除还是永远保留在此?

They will eventually be garbage collected. 他们最终将被垃圾收集。 But you should not think of unreachable commits as a problem, it's perfectly normal to have plenty of those, as there are some after many operations like rebase or reset . 但是您不应该将无法到达的提交视为一个问题,拥有大量的提交是完全正常的,因为在进行诸如rebasereset类的许多操作之后,还需要进行一些操作。

Speaking of rebase : As @TimBiregeleisen mentioned, I think an interactive rebase would be the cleaner solution here: git rebase -i HEAD~2 lets you revise the last two commits. 说到rebase :正如@TimBiregeleisen所提到的,我认为交互式rebase将是这里更干净的解决方案: git rebase -i HEAD~2 HEAD〜2可让您修改最后两个提交。 You can choose to rewrite the commit message, edit the whole commit, squash several commits into one and other things; 您可以选择重写提交消息,编辑整个提交,将多个提交压缩为一个或其他内容。 there is plenty of documentation out there ( here or here for example). 那里有很多文档(例如此处此处 )。

Of course, an interactive rebase creates completely new and shiny commits and leaves the old ones dangling, but, as mentioned above, that's no problem as long as you did not push those old commits and someone else started working on top of them . 当然,交互式的rebase会创建全新的闪亮提交,并使旧的提交悬而未决,但是,如上所述, 只要您不推动这些旧提交并且其他人开始在它们之上工作,那没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM