意外推送提交:更改 git 提交消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5032374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accidentally pushed commit: change git commit message
提问by jonny
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?
你会如何解决这种情况?
回答by Dan Moulding
Easiest solution (but please read this whole answer before doing this):
最简单的解决方案(但请在执行此操作之前阅读整个答案):
git rebase -i <hash-of-commit-preceding-the-incorrect-one>
- In the editor that opens, change
pick
toreword
on the line for the incorrect commit. - Save the file and close the editor.
- The editor will open again with the incorrect commit message. Fix it.
- Save the file and close the editor.
git push --force
to update GitHub.
git rebase -i <hash-of-commit-preceding-the-incorrect-one>
- 在打开的编辑器中,在不正确提交的行中更改
pick
为reword
。 - 保存文件并关闭编辑器。
- 编辑器将再次打开并显示不正确的提交消息。修理它。
- 保存文件并关闭编辑器。
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.
这意味着您将发布先前发布的存储库的修改版本。如果有人在您使用不正确的提交消息犯错和修复它之间从您的 repo 中拉取或提取,那么他们以后会遇到一些困难。因此,在尝试此操作之前,请确保您可以接受此结果。
回答by Abizern
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-resetmanpage.
您可以在git-reset联机帮助页中看到这些选项。
For a project that only you are working on, the changed history shouldn't be a problem.
对于只有您正在处理的项目,更改的历史记录应该不是问题。
回答by sebers
If you have to change an old commit message over multiple branches (i.e., 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. This is necessary if the the temporary directory is already present or if there are already references stored under refs/original. If that is not the case, you can drop this flag.
-f
将强制执行操作。如果临时目录已经存在,或者如果已经在refs/original下存储了引用,则这是必要的。如果不是这种情况,您可以删除此标志。
--
separates filter-branch options from revision options
--
将过滤器分支选项与修订选项分开
--all
will make sure, that all branchesand tagsare 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:
假设你想恢复你的 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.
请注意,您应该将此通知您的合作者,因为从第一个修改的提交开始的所有哈希值都已更改。
回答by Prabhakar Undurthi
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. i.e 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.
如果已经推送,这是更改提交消息时的最佳实践。