更改 Git 上的旧提交消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1884474/
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
Change old commit message on Git
提问by Pablo Fernandez
I was trying to edit an old commit message as explained here.
我试图按照此处的说明编辑旧的提交消息。
The thing is that now, when I try to run rebase -i HEAD~5
it says interactive rebase already started
.
问题是现在,当我尝试运行rebase -i HEAD~5
它时说interactive rebase already started
.
So then I try: git rebase --continue
but got this error:
那么我尝试:git rebase --continue
但得到了这个错误:
error: Ref refs/heads/master is at 7c1645b447a8ea86ee143dd08400710c419b945b but expected c7577b53d05c91026b9906b6d29c1cf44117d6ba
fatal: Cannot lock the ref 'refs/heads/master'.
Any ideas?
有任何想法吗?
采纳答案by VonC
It says:
它说:
When you save and exit the editor, it will rewind you back to that last commit in that list and drop you on the command line with the following message:
当您保存并退出编辑器时,它会将您退回到该列表中的最后一次提交,并将您放到命令行上,并显示以下消息:
$ git rebase -i HEAD~3
Stopped at 7482e0d... updated the gemspec to hopefully work better
You can amend the commit now, with
It does not mean:
这并不意味着:
type again
git rebase -i HEAD~3
再次输入
git rebase -i HEAD~3
Try to nottyping git rebase -i HEAD~3
when exiting the editor, and it should work fine.
(otherwise, in your particular situation, a git rebase -i --abort
might be needed to reset everything and allow you to try again)
退出编辑器时尽量不要输入 git rebase -i HEAD~3
,它应该可以正常工作。
(否则,在您的特定情况下,git rebase -i --abort
可能需要重置所有内容并允许您重试)
As Dave Vogtmentions in the comments, git rebase --continue
is for going to the next task in the rebasing process, after you've amended the first commit.
正如Dave Vogt在评论中提到的,git rebase --continue
是在您修改第一个 commit 之后,转到变基过程中的下一个任务。
Also, Gregg Lindmentions in his answerthe reword
command of git rebase
:
此外,Gregg Lind在他的回答中提到了以下reword
命令git rebase
:
By replacing the command "pick" with the command "edit", you can tell
git rebase
to stop after applying that commit, so that you can edit the files and/or the commit message, amend the commit, and continue rebasing.If you just want to edit the commit message for a commit, replace the command "
pick
" with the command "reword
", since Git1.6.6 (January 2010).It does the same thing ‘
edit
' does during an interactive rebase, except it only lets you edit the commit message without returning control to the shell. This is extremely useful.
Currently if you want to clean up your commit messages you have to:
通过将命令“pick”替换为“edit”命令,您可以告诉
git rebase
在应用该提交后停止,以便您可以编辑文件和/或提交消息、修改提交并继续重新设定基准。如果您只想编辑提交的提交消息,请将命令 "
pick
" 替换为命令 "reword
",因为Git1.6.6 (January 2010)。它
edit
在交互式变基期间执行相同的操作 ' ',除了它只允许您编辑提交消息而不将控制权返回给 shell。这是非常有用的。
目前,如果您想清理提交消息,您必须:
$ git rebase -i next
Then set all the commits to ‘edit'. Then on each one:
然后将所有提交设置为“编辑”。然后在每一个上:
# Change the message in your editor.
$ git commit --amend
$ git rebase --continue
Using ‘
reword
' instead of ‘edit
' lets you skip thegit-commit
andgit-rebase
calls.
使用 '
reword
' 而不是 'edit
' 可以让您跳过git-commit
和git-rebase
调用。
回答by Gregg Lind
FWIW, git rebase interactive now has a "reword" option, which makes this much less painful!
FWIW,git rebase interactive 现在有一个“reword”选项,这让这变得不那么痛苦了!
回答by Punit Vara
As Gregg Lind suggested, you can use rewordto be prompted to only change the commit message (and leave the commit intact otherwise):
正如 Gregg Lind 建议的那样,您可以使用reword来提示仅更改提交消息(否则保持提交不变):
git rebase -i HEAD~n
Here, n
is the list of last n commits.
这n
是最近 n 次提交的列表。
For example, if you use git rebase -i HEAD~4
, you may see something like this:
例如,如果您使用git rebase -i HEAD~4
,您可能会看到如下内容:
pick e459d80 Do xyz
pick 0459045 Do something
pick 90fdeab Do something else
pick facecaf Do abc
Now replace pickwith rewordfor the commits you want to edit the messages of:
现在用reword替换你想要编辑的提交的pick消息:
pick e459d80 Do xyz
reword 0459045 Do something
reword 90fdeab Do something else
pick facecaf Do abc
Exit the editor after saving the file, and next you will be prompted to edit the messages for the commits you had marked reword, in one file per message. Note that it would've been much simpler to just edit the commit messages when you replaced pick
with reword
, but doing that has no effect.
保存文件后退出编辑器,接下来将提示您编辑标记为reword的提交的消息,每条消息在一个文件中。请注意,当您替换pick
为时,仅编辑提交消息会简单得多reword
,但这样做没有任何效果。
Learn more on GitHub's page for Changing a commit message.
回答by WebDever
Just wanted to provide a different option for this. In my case, I usually work on my individual branches then merge to master, and the individual commits I do to my local are not that important.
只是想为此提供一个不同的选择。就我而言,我通常在我的各个分支上工作,然后合并到 master,而我对本地所做的个人提交并不那么重要。
Due to a git hook that checks for the appropriate ticket number on Jira but was case sensitive, I was prevented from pushing my code. Also, the commit was done long ago and I didn't want to count how many commits to go back on the rebase.
由于 git hook 在 Jira 上检查适当的票号但区分大小写,我无法推送我的代码。此外,提交是很久以前完成的,我不想计算要返回变基的提交次数。
So what I did was to create a new branch from latest master and squash all commits from problem branch into a single commit on new branch. It was easier for me and I think it's good idea to have it here as future reference.
所以我所做的是从最新的 master 创建一个新分支,并将问题分支的所有提交压缩到新分支上的单个提交中。这对我来说更容易,我认为把它放在这里作为将来的参考是个好主意。
From latest master:
来自最新大师:
git checkout -b new-branch
Then
然后
git checkout new-branch
git merge --squash problem-branch
git commit -m "new message"
参考:https://github.com/rotati/wiki/wiki/Git:-Combine-all-messy-commits-into-one-commit-before-merging-to-Master-branch
回答by Mahmoud Zalt
Here's a very nice Gist that covers all the possible cases: https://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4
这是一个非常好的 Gist,涵盖了所有可能的情况:https: //gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4
Overview:
概述:
git rebase -i HEAD~X
# X is the number of commits to go back
# Move to the line of your commit, change pick into edit,
# then change your commit message:
git commit --amend
# Finish the rebase with:
git rebase --continue