简体   繁体   English

意外推送提交:更改git提交消息

[英]Accidentally pushed commit: change git commit message

In my local repo I have one commit with an incorrect commit message. 在我的本地仓库中,我有一个提交错误的提交消息。

I've already published the incorrect commit message with git push . 我已经使用git push发布了错误的提交消息。

Now the remote repo (which is GitHub-hosted) has the incorrect commit message, too. 现在,远程仓库(GitHub托管的)也有不正确的提交消息。

I've already tried git commit --amend , but found that it will not work for me in this situation because I've made additional commits since the incorrect one. 我已经尝试过git commit --amend ,但发现在这种情况下它对我不起作用,因为我已经做了额外的提交,因为不正确的提交。

How would you fix this situation? 你会如何解决这种情况?

Easiest solution ( but please read this whole answer before doing this ): 最简单的解决方案( 但请在完成此操作之前阅读完整的答案 ):

  1. git rebase -i <hash-of-commit-preceding-the-incorrect-one>
  2. In the editor that opens, change pick to reword on the line for the incorrect commit. 在打开的编辑器中,将pick更改为reword行以进行不正确的提交。
  3. Save the file and close the editor. 保存文件并关闭编辑器。
  4. The editor will open again with the incorrect commit message. 编辑器将使用不正确的提交消息再次打开。 Fix it. 修理它。
  5. Save the file and close the editor. 保存文件并关闭编辑器。
  6. git push --force to update GitHub. git push --force更新GitHub。

This will mean you will be publishing a modified version of a previously published repository. 这意味着您将发布以前发布的存储库的修改版本。 If anyone pulled or fetched from your repo between when you made the mistake with the incorrect commit message, and when you fixed it, then they will experience some difficulties later. 如果有人在您使用不正确的提交消息犯错误之间从您的仓库中提取或取出,并且当您修复它时,他们将在以后遇到一些困难。 So be sure you can accept this consequence before trying this. 所以在尝试之前一定要接受这个后果。

Rather than go the whole rebase route for one commit: 而不是为一个提交去整个rebase路线:

git reset --soft head~
git commit -m "The message you wanted to use"
git push -f

You can see the options in the git-reset manpage. 您可以在git-reset手册页中看到这些选项。

For a project that only you are working on, the changed history shouldn't be a problem. 对于只有您正在处理的项目,更改的历史记录应该不是问题。

If you have to change an old commit message over multiple branches (ie, the commit with the erroneous message is present in multiple branches) you might want to use 如果您必须在多个分支上更改旧的提交消息(即,多个分支中存在错误消息的提交),您可能希望使用

git filter-branch -f --msg-filter 'sed "s/<old message>/<new message>/g"' -- --all

to replace the commit message. 替换提交消息。

Git will create a temporary directory for rewriting and additionally backup old references in refs/original/. Git将创建一个临时目录进行重写,并在refs / original /中另外备份旧引用。

-f will enforce the execution of the operation. -f将强制执行操作。 This is necessary if the the temporary directory is already present or if there are already references stored under refs/original . 如果临时目录已存在或者已存在refs / original下存储的引用,则这是必需的。 If that is not the case, you can drop this flag. 如果不是这种情况,您可以删除此标志。

-- separates filter-branch options from revision options --将过滤器分支选项与修订选项分开

--all will make sure, that all branches and tags are rewritten. --all将确保所有分支标签都被重写。

Due to the backup of your old references, you can easily go back to the state before executing the command. 由于旧引用的备份,您可以在执行命令之前轻松返回状态。

Say, you want to recover your master and access it in branch old_master: 说,您想要恢复您的主人并在分支old_master中访问它:

git checkout -b old_master refs/original/refs/heads/master

After you are satisfied with your changes use git push -f to push the changes to your public repo. 对您的更改感到满意后,使用git push -f将更改推送到您的公共存储库。

Note that you should inform your collaborators about this since all the hashes of the commits starting with the first modified one have been changed. 请注意,您应该通知您的协作者,因为已经更改了从第一个修改过的提交开始的所有哈希值。

If you're not pushed the code to your remote branch(Github/Bitbucket) you can change the commit message on the command line as below. 如果您没有将代码推送到远程分支(Github / Bitbucket),您可以在命令行上更改提交消息,如下所示。

 git commit --amend -m "Your new message"

If you're working on a specific branch do this. 如果您正在处理特定分支,请执行此操作。

git commit --amend -m "BRANCH-NAME: new message"

If you've already pushed the code with wrong message then you need to be careful when changing the message. 如果您已经使用错误的消息推送了代码,那么在更改消息时需要小心。 ie after you change the commit message and try pushing it again you end up with having issues. 即在您更改提交消息并尝试再次推送之后,您最终会遇到问题。 To make it smooth follow these steps. 为了顺利完成这些步骤。 Please read the entire answer before doing it 请在完成之前阅读完整的答案

git commit --amend -m "BRANCH-NAME : your new message"

git push -f origin BRANCH-NAME                # Not a best practice. Read below why?

Important note: When you use the force push directly you might end up with code issues that other developers are working on the same branch. 重要说明:直接使用强制推送时,最终可能会遇到其他开发人员在同一分支上工作的代码问题。 So to avoid that conflicts you need to pull the code from your branch before making the force push 因此,为了避免这种冲突,您需要在强制推送之前从分支中提取代码

 git commit --amend -m "BRANCH-NAME : your new message"
 git pull origin BRANCH-NAME
 git push -f origin BRANCH-NAME

This is the best practice when changing the commit message, if it was already pushed. 这是更改提交消息时的最佳做法(如果已经推送)。

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

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