181

我试图编辑旧的提交消息,如此所述。

问题是,现在,当我尝试运行rebase -i HEAD~5它时,它会说interactive rebase already started.

所以然后我尝试:git rebase --continue但收到此错误:

error: Ref refs/heads/master is at 7c1645b447a8ea86ee143dd08400710c419b945b but expected c7577b53d05c91026b9906b6d29c1cf44117d6ba
fatal: Cannot lock the ref 'refs/heads/master'.

有任何想法吗?

4

5 回答 5

164

它说:

当您保存并退出编辑器时,它会将您倒回到该列表中的最后一次提交,并将您放到命令行中,并显示以下消息:

$ git rebase -i HEAD~3
Stopped at 7482e0d... updated the gemspec to hopefully work better
You can amend the commit now, with

这并不意味着:

再次输入 git rebase -i HEAD~3

退出编辑器时尽量不要输入 ,它应该可以正常工作。 (否则,在您的特定情况下,可能需要重置所有内容并允许您重试)git rebase -i HEAD~3
git rebase -i --abort


正如Dave Vogt在评论中提到的那样,在您修改了第一个 commit 之后git rebase --continue,用于在变基过程中执行下一个任务。

此外,Gregg Lind在他的回答中提到了以下reword命令git rebase

通过将命令“pick”替换为命令“edit”,您可以git rebase在应用该提交后告诉停止,以便您可以编辑文件和/或提交消息,修改提交,并继续变基。

如果您只想编辑提交的提交消息,请将命令 " pick" 替换为命令 " reword",因为Git1.6.6 (January 2010)

它与 '<code>edit' 在交互式 rebase 期间执行的操作相同,只是它只允许您编辑提交消息而不将控制权返回给 shell。这是非常有用的。
目前,如果您想清理提交消息,您必须:

$ git rebase -i next

然后将所有提交设置为“编辑”。然后在每一个上:

# Change the message in your editor.
$ git commit --amend
$ git rebase --continue

使用 '<code>reword' 而不是 '<code>edit' 可以让您跳过git-commitandgit-rebase调用

于 2009-12-10T22:37:52.750 回答
126

正如 Gregg Lind 建议的那样,您可以使用reword来提示您仅更改提交消息(否则保持提交不变):

git rebase -i HEAD~n

这里n是最后 n 次提交的列表。

例如,如果您使用git rebase -i HEAD~4,您可能会看到如下内容:

pick e459d80 Do xyz
pick 0459045 Do something
pick 90fdeab Do something else
pick facecaf Do abc

现在将pick替换为reword,用于您要编辑以下消息的提交:

pick e459d80 Do xyz
reword 0459045 Do something
reword 90fdeab Do something else
pick facecaf Do abc

保存文件后退出编辑器,接下来将提示您编辑标记为reword的提交的消息,每条消息在一个文件中。pick请注意,当您替换为时,仅编辑提交消息会简单得多reword,但这样做没有任何效果。

在 GitHub 的更改提交消息页面上了解更多信息。

于 2017-07-25T12:02:49.133 回答
61

FWIW,git rebase interactive 现在有了一个reword选项,这让这变得不那么痛苦了!

于 2010-07-26T15:42:57.460 回答
17

只是想为此提供一个不同的选择。就我而言,我通常在我的各个分支上工作,然后合并到 master,而我对本地所做的各个提交并不那么重要。

由于一个 git 钩子会检查 Jira 上的相应票号但区分大小写,因此我无法推送我的代码。此外,提交是很久以前完成的,我不想计算有多少提交返回到 rebase。

所以我所做的是从最新的 master 创建一个新分支,并将问题分支中的所有提交压缩到新分支上的单个提交中。这对我来说更容易,我认为把它放在这里作为将来的参考是个好主意。

来自最新大师:

git checkout -b 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

于 2019-01-08T14:09:09.807 回答
11

这是一个非常好的要点,涵盖了所有可能的情况:https ://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4

概述:

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
于 2018-11-23T10:42:05.067 回答