使用“git rebase”更改旧的提交消息

发布于 2024-08-14 11:52:17 字数 450 浏览 8 评论 0原文

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

问题是,现在,当我尝试运行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

Hello爱情风 2024-08-21 11:52:17

正如 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 页面上的更改提交消息

As Gregg Lind suggested, you can use reword to be prompted to only change the commit message (and leave the commit intact otherwise):

git rebase -i HEAD~n

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:

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

Now replace pick with reword for the commits you want to edit the messages of:

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.

Learn more on GitHub's page for Changing a commit message.

苹果你个爱泡泡 2024-08-21 11:52:17

它说:

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

$ 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 在评论中提到,git rebase --Continue 用于执行变基过程中的下一个任务,在您之后'已经修改了第一个提交

另外,Gregg Lind他的回答git rebase

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

如果您只想编辑提交的提交消息,请将命令“pick”替换为命令“reword,自 Git1.6.6(2010 年 1 月)起.

它与“edit”在交互式变基期间执行的操作相同,只不过它只允许您编辑提交消息,而不将控制权返回到 shell。这非常有用。
目前,如果您想清理提交消息,您必须:

$ git rebase -i next

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

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

使用“reword”而不是“edit”可以跳过 git-commitgit-rebase 调用

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

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 of 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:

$ 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 the git-commit and git-rebase calls.

蘸点软妹酱 2024-08-21 11:52:17

FWIW,git rebase Interactive 现在有一个 reword 选项,这使得这不再那么痛苦!

FWIW, git rebase interactive now has a reword option, which makes this much less painful!

爱的十字路口 2024-08-21 11:52:17

要更改历史记录中任何位置的提交消息:

1- git rebase -i是要更改的提交之前的 SHA 提交(此处: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):
enter image description here

2- change pick (default) to reword 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
enter image description here

and that's it, now the history is modified and a git push --force-with-lease will replace on remote

罪#恶を代价 2024-08-21 11:52:17

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

由于 git hook 检查 Jira 上适当的票号但区分大小写,因此我无法推送我的代码。另外,提交很久以前就完成了,我不想计算有多少提交要返回到 rebase 上。

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

来自最新大师:

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

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:

git checkout -b new-branch

Then

git merge --squash problem-branch
git commit -m "new message" 

Referece:
https://github.com/rotati/wiki/wiki/Git:-Combine-all-messy-commits-into-one-commit-before-merging-to-Master-branch

鹿港巷口少年归 2024-08-21 11:52:17

这是一个非常好的要点,涵盖了所有可能的情况: 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

Here's a very nice Gist that covers all the possible cases: 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文