推送后更改 git 提交消息(假设没有人从远程拉取)
我已经进行了 git 提交和后续推送。我想更改提交消息。如果我理解正确,这是不可取的,因为在我进行此类更改之前可能有人已经从远程存储库中提取了内容。如果我知道没有人拉动怎么办?
有办法做到这一点吗?
I have made a git commit and subsequent push. I would like to change the commit message. If I understand correctly, this is not advisable because someone might have pulled from the remote repository before I make such changes. What if I know that no one has pulled?
Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
更改历史记录
如果这是最近的提交,您可以简单地执行以下操作:
这将打开带有最后提交消息的编辑器,并让您编辑该消息。 (如果您想清除旧消息并使用新消息,可以使用
-m
。)推送
然后当您推送时,执行以下操作:
或者您可以使用“+”:
或者您可以use
--force
:使用这些命令时要小心。
如果其他人将更改推送到同一分支,您可能希望避免破坏这些更改。
--force-with-lease
选项是最安全的,因为如果有任何上游更改,它将中止(如果您没有显式指定分支,Git 将会中止)如果您使用默认推送设置,则使用默认推送设置。是“匹配”,那么您可以同时销毁多个分支上的更改
之后拉取/提取的
任何人现在都会收到一条错误消息,并且他们需要通过执行类似的操作来更新(假设他们自己没有进行任何更改)。 :
使用
reset时要小心--hard
如果您对分支进行了更改,这些更改将被销毁。关于修改历史记录的说明
被破坏的数据实际上只是旧的提交消息,但
--force
不知道这一点,并且也会很乐意删除其他数据。因此,可以将--force
视为“我想要销毁数据,并且我确切地知道哪些数据正在被销毁。”但是,当提交被破坏的数据时,您通常可以从引用日志中恢复旧的提交——数据实际上是孤立的,而不是被破坏的(尽管孤立的提交会定期删除)。如果您认为自己没有破坏数据,请远离
--force
...可能会发生不好的事情。这就是为什么
--force-with-lease
更安全。Changing history
If it is the most recent commit, you can simply do this:
This brings up the editor with the last commit message and lets you edit the message. (You can use
-m
if you want to wipe out the old message and use a new one.)Pushing
And then when you push, do this:
Or you can use "+":
Or you can use
--force
:Be careful when using these commands.
If someone else pushed changes to the same branch, you probably want to avoid destroying those changes. The
--force-with-lease
option is the safest, because it will abort if there are any upstream changes (If you don't specify the branch explicitly, Git will use the default push settings. If your default push setting is "matching", then you may destroy changes on several branches at the same time.
Pulling / fetching afterwards
Anyone who already pulled will now get an error message, and they will need to update (assuming they aren't making any changes themselves) by doing something like this:
Be careful when using
reset --hard
. If you have changes to the branch, those changes will be destroyed.A note about modifying history
The destroyed data is really just the old commit message, but
--force
doesn't know that, and will happily delete other data too. So think of--force
as "I want to destroy data, and I know for sure what data is being destroyed." But when the destroyed data is committed, you can often recover old commits from the reflog—the data is actually orphaned instead of destroyed (although orphaned commits are periodically deleted).If you don't think you're destroying data, then stay away from
--force
... bad things might happen.This is why
--force-with-lease
is somewhat safer.只需说:
然后
Just say:
and then
要编辑除最新之外的提交:
第 1 步:
git rebase -i HEAD~n
对受影响的最后n
提交进行交互式变基。 (即如果你想更改提交消息 3 次提交,请执行 git rebase -i HEAD~3 )git 将弹出一个编辑器来处理这些提交,请注意这个命令:
这正是我们需要的!
第 2 步:对于您想要更新消息的提交,将
pick
更改为r
。不要在此处更改提交消息,它将被忽略。您将在下一步中执行此操作。保存并关闭编辑器。请注意,如果您编辑变基“计划”,但它不会开始让您重命名文件的过程,请运行:
如果您想更改用于交互式会话的文本编辑器(例如从默认的 vi 到 nano), run:
Step3:Git 将为您之前输入
r
的每个修订版弹出另一个编辑器。根据需要更新提交消息,然后保存并关闭编辑器。第四步:更新所有提交消息后。您可能需要执行 git push -f 来更新遥控器。
To edit a commit other than the most recent:
Step1:
git rebase -i HEAD~n
to do interactive rebase for the lastn
commits affected. (i.e. if you want to change a commit message 3 commits back, dogit rebase -i HEAD~3
)git will pop up an editor to handle those commits, notice this command:
that is exactly we need!
Step2: Change
pick
tor
for those commits that you want to update the message. Don't bother changing the commit message here, it will be ignored. You'll do that on the next step. Save and close the editor.Note that if you edit your rebase 'plan' yet it doesn't begin the process of letting you rename the files, run:
If you want to change the text editor used for the interactive session (e.g. from the default vi to nano), run:
Step3: Git will pop up another editor for every revision you put
r
before. Update the commit msg as you like, then save and close the editor.Step4: After all commits msgs are updated. you might want to do
git push -f
to update the remote.在控制台中使用以下两个步骤:
然后
完成:)
Use these two steps in console:
and then
Done :)
情况 1:未推送 + 最近提交:
这将打开您的
$EDITOR
并让您更改消息。继续您常用的git Push origin master
。情况 2:已推送 + 最近提交:
我们像上面一样编辑消息。但需要
--force
推送来更新远程历史记录。⚠️但是!更改后强制推送您的提交很可能会阻止其他人与存储库同步(如果他们已经提取了副本)。您应该首先与他们核实。
情况 3:未推送 + 旧提交:
Rebase 打开您的历史记录并让您选择要更改的内容。通过编辑,您可以告诉您要更改消息。 Git 将您移动到一个新分支,以便您修改消息。 git rebase --Continue 将您带回到之前的分支,并且消息已更改。
情况 4:已推送 + 旧提交:
使用与上述相同的 3 个步骤流程编辑消息(
rebase -i
、commit --amend
,变基--继续
)。然后强制推送提交:
⚠️但是!请记住,在更改后重新推送您的提交很可能会阻止其他人与存储库同步(如果他们已经提取了副本)。您应该首先与他们核实。
参考:https://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4#not-pushed--most-recent-commit
Case 1 : Not pushed + most recent commit:
This will open your
$EDITOR
and let you change the message. Continue with your usualgit push origin master
.Case 2 : Already pushed + most recent commit:
We edit the message like just above. But need to
--force
the push to update the remote history.⚠️ But! Force pushing your commit after changing it will very likely prevent others to sync with the repo, if they already pulled a copy. You should first check with them.
Case 3 : Not pushed + old commit:
Rebase opens your history and let you pick what to change. With edit you tell you want to change the message. Git moves you to a new branch to let you --amend the message. git rebase --continue puts you back in your previous branch with the message changed.
Case 4 : Already pushed + old commit:
Edit your message with the same 3 steps process as above (
rebase -i
,commit --amend
,rebase --continue
).Then force push the commit:
⚠️ But! Remember re-pushing your commit after changing it will very likely prevent others to sync with the repo, if they already pulled a copy. You should first check with them.
Reference : https://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4#not-pushed--most-recent-commit
应该注意的是,如果您将
push --force
与多个引用一起使用,它们都会因此被修改。请务必注意您的 git 存储库的位置配置为推送到。幸运的是,有一种方法可以通过指定要更新的单个分支来稍微保护该过程。从 git 手册页中阅读:It should be noted that if you use
push --force
with mutiple refs, they will ALL be modified as a result. Make sure to pay attention to where your git repo is configured to push to. Fortunately there is a way to safeguard the process slightly, by specifying a single branch to update. Read from the git man pages:命令1。
然后,
命令2。
Command 1.
Then,
Command 2.
然后在当前窗口中编辑和更改消息。之后做
then edit and change the message in the current window. After that do
确保您在正确的分支上进行更改
#确保您在正确的分支上进行更改
只是为了确定:
然后
然后推
To make sure you are making changes on the right branch
#to make sure you are making changes on the right branch
just to be sure:
Then
Then push
如果您想修改较旧的提交,而不是最后一个提交,则需要使用
rebase
命令,如下所述,Github 帮助页面,位于修改旧提交消息或多个提交消息部分If you want to modify an older commit, not the last one, you will need to use
rebase
command as explained in here,Github help page , on the Amending the message of older or multiple commit messages section可以用不同的消息替换提交,而不影响现有的提交哈希值,而不是变基和强制推送修改后的分支:
然后必须显式推送和获取修改后的引用:
这需要 Git 2.1 或更高版本。
https://git-scm.com/docs/git-replace
Instead of rebasing and force pushing the modified branch, it's possible to replace a commit with a different message without affecting the existing commit hashes:
The modified references will then have to be pushed and fetched explicitly with:
This requires Git 2.1 or higher.
https://git-scm.com/docs/git-replace
另一种选择是创建一个额外的“勘误提交”(并推送),它引用包含错误的提交对象——新的勘误提交还提供了更正。勘误提交是一种没有实质性代码更改但有重要提交消息的提交 - 例如,在自述文件中添加一个空格字符并使用重要提交消息提交该更改,或者使用 git 选项
--allow -空
。它当然比变基更容易、更安全,它不会修改真实的历史记录,并且可以保持分支树干净(如果您要更正最近的提交,那么使用amend
也是一个不错的选择,但是勘误表对于较旧的提交来说,提交可能是一个不错的选择)。这种类型的事情很少发生,只需记录错误就足够了。将来,如果您需要在 git 日志中搜索某个功能关键字,则原始(错误)提交可能不会出现,因为原始提交中使用了错误的关键字(原始拼写错误)——但是,该关键字将会出现在勘误表提交中,它会将您指向有拼写错误的原始提交。这是一个例子:Another option is to create an additional "errata commit" (and push) which references the commit object that contains the error -- the new errata commit also provides the correction. An errata commit is a commit with no substantive code changes but an important commit message -- for example, add one space character to your readme file and commit that change with the important commit message, or use the git option
--allow-empty
. It's certainly easier and safer than rebasing, it doesn't modify true history, and it keeps the branch tree clean (usingamend
is also a good choice if you are correcting the most recent commit, but an errata commit may be a good choice for older commits). This type of thing so rarely happens that simply documenting the mistake is good enough. In the future, if you need to search through a git log for a feature keyword, the original (erroneous) commit may not appear because the wrong keyword was used in that original commit (the original typo) -- however, the keyword will appear in the errata commit which will then point you to the original commit that had the typo. Here's an example:只需使用这 2 个命令即可更改上次推送的提交消息
Simply use this 2 commands to change the commit message of your last push
FWIW 在最简单的情况下,检查的答案是正确的。我只是想分享一下最近的经历。
这有效。
git commit --amend -m"The new message here"
git push --force origin
测试人员能够第一次检出分支,并且仅修改后的消息。
因此,在最简单的情况下(单个开发人员和/或知道没有人获取/拉取或签出分支)就足够了。
FWIW in the simplest case, the checked answer is correct. I just wanted to share a recent experience.
This worked.
git commit --amend -m"The new message here"
git push --force origin
And the tester was able to checkout the branch for the first time and has only the amended message.
So in the simplest case (single developer and/or known that no one has fetch/pulled or checked out the branch) that is sufficient.
工作
我们有 git amend 命令,使用它我们可以单独更新提交消息或与最后提交的一部分的附加文件一起更新,所以我们必须使用
然后强制推送,这样即使提交到本地或远程它将更新两者
Working
We have git amend command using which we can update commit message alone or along with additional files which will be part of last commit so we have to use
and then push with force so that even though commited to local or remote it will update both
命令 1
您需要使用以下命令更改提交消息
命令 2
添加新消息后,然后执行以下命令
Command 1
You need to change your commit message use the Below command
Command 2
After the add a new message and then below command execute
如果您使用以下 -S 参数提交 Wearg 消息,则事件将起作用:
此时不要忘记您添加了更改并且您的遥控器不同,这就是您需要执行的原因:
如果您在 VSCode 中,您将看到差异, --force 参数忽略 git pull 并将更改的消息推送到同一个提交。
Event if you commit worng message with -S parameter following would work:
at this point do not forget that you added a change and your remote is different, this why you need to do:
If you are in VSCode you will see the difference, with --force parameter you ignore git pull and you push your changed message to the same commit.
我对 Git 有点陌生,但我只是想补充一下我的经验。
这很好用,但下一个对我来说是问题。
在更改提交消息之前我已经推送了提交。
最后,当我尝试推送到远程时,它抛出了异常。
所以我应该在更新远程分支之前再次下拉。
希望我的一点经验对你有帮助。
谢谢。
I'm a little bit new to Git, but I just wanna add my experience.
This worked great but the next was the problem for me.
I already pushed the commit before changing the commit message.
Finally, when I tried to push to the remote, it git threw an exception.
So I should have pull down again before updating the remote branch.
Hope my minor experience helps you.
Thanks.
这对我来说非常好,
git checkout origin/branchname
如果您已经在分支中,那么最好执行 pull 或 rebase
或
稍后您可以简单地使用,
或者如果您想打开文本编辑器,则使用
我更喜欢使用文本-如果您有很多意见,请编辑。您可以使用命令设置您喜欢的文本编辑器
无论如何,当您完成更改提交消息后,保存并退出
然后运行
就完成了
This works for me pretty fine,
git checkout origin/branchname
if you're already in branch then it's better to do pull or rebase
or
Later you can simply use
or if you like to open text-editor then use
I will prefer using text-editor if you have many comments. You can set your preferred text-editor with command
Anyway, when your are done changing the commit message, save it and exit
and then run
And you're done
如果您使用的是 bitbucket 管道,则相同问题的其他信息
编辑您的消息
推送到服务器,
然后将 --force 添加到管道上的推送命令
这将删除您以前的提交并推送您当前的提交一。
我在 bitbucket 管道上尝试过它并且工作正常
additional information for same problem if you are using bitbucket pipeline
edit your message
push to the sever
then add --force to your push command on the pipeline
This will delete your previous commit(s) and push your current one.
i tried it on bitbucket pipeline and its working fine
我第一次尝试重命名大约 6 个已经推送的旧提交消息,此后我做了进一步的提交。
所以这是最糟糕的情况“情况 4:已推送 + 旧提交”。
我是 Vim(现在是 Nvim)用户,也是 Vim's Fugitive 的超级粉丝。我就是这样做的。
这是关于 在 Fugitive 中变基的一般帮助:
获取提交列表。
:Git log --oneline
我想修复第 12 行的提交消息(
4d43a1b build(MPL-402): editorconfig fix for messages.json
),因为它的格式不正确。我将光标放在提交哈希
4d43a1b
上并输入 rw这将“执行交互式变基,并将光标下的提交设置为
reword
。”。请注意,与 git rebase -i HEAD~X 相比,这有多好 - 知道 X 是什么并不那么简单。现在应该为您提供正确的
git rebase
命令。因此,使用:wq
写入并退出该缓冲区
然后按照 git/fugitive 的指导进行操作。我的一个提交出现了合并冲突,我通过正常的 逃亡合并冲突过程。
希望这足以让您克服第一个“我以前从未做过变基,我到底应该做什么?”栏。如果您需要后续步骤的帮助,请给我留言。
I did my first attempt at renaming about 6 old commit messages that were already pushed and I had since done further commits.
So it's the nastiest case 'Case 4 : Already pushed + old commit'.
I'm a Vim (Nvim now) user and a mega fan of Vim's Fugitive. So this is how I did it with that.
This is the general help that you have around rebasing in Fugitive:
Get a list of commits.
:Git log --oneline
I want to fix the commit message on line 12 (
4d43a1b build(MPL-402): editorconfig fix for messges.json
), because its not in the proper Conventional Commit format.I put my cursor over the commit hash
4d43a1b
and type rwThis will "Perform an interactive rebase with the commit under the cursor set to
reword
.". Note how nice this is compared togit rebase -i HEAD~X
- knowing what thatX
is is not so simple.This should now give you the correct
git rebase
command. So write and quit that buffer with:wq
Then follow the steps through as git/fugitive guides you. I got a merge conflict for one of my commits which I fixed through my normal fugitve merge conflict process.
Hopefully that's enough to get you over the first "I've never done a rebase before, what the hell am I supposed to do?" hurdle. Leave me a comment if you want help with the later steps.