使用“git rebase”更改旧的提交消息
我试图编辑旧的提交消息,如此处所述。
问题是,现在,当我尝试运行rebase -i HEAD~5
时,它说交互式rebase已经开始
。
然后我尝试: git rebase --continue
但收到此错误:
error: Ref refs/heads/master is at 7c1645b447a8ea86ee143dd08400710c419b945b but expected c7577b53d05c91026b9906b6d29c1cf44117d6ba
fatal: Cannot lock the ref 'refs/heads/master'.
有什么想法吗?
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
.
So then I try: git rebase --continue
but got this error:
error: Ref refs/heads/master is at 7c1645b447a8ea86ee143dd08400710c419b945b but expected c7577b53d05c91026b9906b6d29c1cf44117d6ba
fatal: Cannot lock the ref 'refs/heads/master'.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如 Gregg Lind 建议的那样,您可以使用 reword 来提示仅更改提交消息(否则保持提交不变):
这里,
n
是最后 n 的列表承诺。例如,如果您使用 git rebase -i HEAD~4 ,您可能会看到类似这样的内容:
现在将提交的 pick 替换为 reword您想要编辑以下消息:
保存文件后退出编辑器,接下来系统将提示您编辑已标记为reword的提交的消息,每条消息在一个文件中。请注意,当您将
pick
替换为reword
时,仅编辑提交消息会简单得多,但这样做没有效果。如需了解更多信息,请访问 GitHub 页面上的更改提交消息。
As Gregg Lind suggested, you can use reword to be prompted to only change the commit message (and leave the commit intact otherwise):
Here,
n
is the list of last n commits.For example, if you use
git rebase -i HEAD~4
, you may see something like this:Now replace pick with reword for the commits you want to edit the messages of:
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
withreword
, but doing that has no effect.Learn more on GitHub's page for Changing a commit message.
它说:
这并不意味着:
退出时尝试不输入
git rebase -i HEAD~3
编辑器,它应该可以正常工作。(否则,在您的特定情况下,可能需要 git rebase -i --abort 来重置所有内容并允许您重试)
如 Dave Vogt 在评论中提到,
git rebase --Continue
用于执行变基过程中的下一个任务,在您之后'已经修改了第一个提交。另外,Gregg Lind 在 他的回答
git rebase
:It says:
It does not mean:
Try to not typing
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)As Dave Vogt mentions in the comments,
git rebase --continue
is for going to the next task in the rebasing process, after you've amended the first commit.Also, Gregg Lind mentions in his answer the
reword
command ofgit rebase
:FWIW,git rebase Interactive 现在有一个 reword 选项,这使得这不再那么痛苦!
FWIW, git rebase interactive now has a
reword
option, which makes this much less painful!要更改历史记录中任何位置的提交消息:
1-是要更改的提交之前的 SHA 提交(此处:
git rebase -i
、eb232eb6b
):2- 将第一行中的
pick
(默认)更改为reword
(不要编辑消息本身)3- 保存并退出
4- 接下来'将再次看到编辑器仅包含旧的提交消息行,因此对其进行编辑,然后保存并退出
就是这样,现在历史记录已修改,
git push --force-with-lease
将在远程替换To change a commit message anywhere in history:
1-
git rebase -i <commit_sha>
, <commit_sha> is the SHA one commit before the commit to be changed (here:eb232eb6b
):2- change
pick
(default) toreword
in the first line (do not edit message itself)3- save and exit
4- next you'll see the editor again with the old commit message line alone, so edit it and then save and exit
and that's it, now the history is modified and a
git push --force-with-lease
will replace on remote只是想为此提供一个不同的选择。就我而言,我通常在单独的分支上工作,然后合并到主分支,而我对本地所做的单独提交并不那么重要。
由于 git hook 检查 Jira 上适当的票号但区分大小写,因此我无法推送我的代码。另外,提交很久以前就完成了,我不想计算有多少提交要返回到 rebase 上。
所以我所做的就是从最新的主分支创建一个新分支,并将问题分支的所有提交压缩为新分支上的单个提交。这对我来说更容易,我认为将其放在这里作为将来的参考是个好主意。
来自最新大师:
然后
参考:
https://github.com/rotati/wiki/wiki/Git:-Combine-all-messy-commits-into-one-commit-before-merging-to-Master-branch
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.
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.
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.
From latest master:
Then
Referece:
https://github.com/rotati/wiki/wiki/Git:-Combine-all-messy-commits-into-one-commit-before-merging-to-Master-branch
这是一个非常好的要点,涵盖了所有可能的情况: https://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4概述
:
Here's a very nice Gist that covers all the possible cases: https://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4
Overview: