git 如何修改现有的、未推送的提交消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/179123/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 05:51:39  来源:igfitidea点击:

How to modify existing, unpushed commit messages?

gitgit-commitgit-rewrite-historyamend

提问by Laurie Young

I wrote the wrong thing in a commit message.

我在提交消息中写了错误的内容。

How can I change the message? The commit has not been pushed yet.

如何更改消息?提交尚未推送。

回答by EfForEffort

Amending the most recent commit message

修改最近的提交信息

git commit --amend

will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:

将打开您的编辑器,允许您更改最近提交的提交消息。此外,您可以直接在命令行中设置提交消息:

git commit --amend -m "New commit message"

…however, this can make multi-line commit messages or small corrections more cumbersome to enter.

...然而,这会使多行提交消息或小的更正输入变得更加麻烦。

Make sure you don't have any working copy changes stagedbefore doing this or they will get committed too. (Unstagedchanges will not get committed.)

确保你没有任何工作副本的修改上演这样做之前,否则它们可能会犯了。(未暂存的更改不会被提交。)

Changing the message of a commit that you've already pushed to your remote branch

更改已推送到远程分支的提交消息

If you've already pushed your commit up to your remote branch, then - after amending your commit locally (as described above) - you'll also need to force push the commitwith:

如果您已经将提交推送到远程分支,那么 - 在本地修改提交后(如上所述) - 您还需要使用以下命令强制推送提交

git push <remote> <branch> --force
# Or
git push <remote> <branch> -f

Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you willlose those commits.

警告:强制推送将使用本地分支的状态覆盖远程分支。如果远程分支上存在您本地分支中没有的提交,您丢失这些提交。

Warning: be cautious about amending commits that you have already shared with other people.Amending commits essentially rewritesthem to have different SHAIDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.

警告:修改您已经与其他人共享的提交时要谨慎。修改提交实质上是将它们重写为具有不同的SHAID,如果其他人拥有您重写的旧提交的副本,则会出现问题。任何拥有旧提交副本的人都需要将他们的工作与您新重写的提交同步,这有时会很困难,因此请确保在尝试重写共享提交历史时与他人协调,或者避免重写共享提交共。



Perform an interactive rebase

执行交互式变基

Another option is to use interactive rebase. This allows you to edit any message you want to update even if it's not the latest message.

另一种选择是使用交互式变基。这允许您编辑要更新的任何消息,即使它不是最新消息。

In order to do a Git squash, follow these steps:

要执行 Git 压缩,请按照下列步骤操作:

// n is the number of commits up to the last commit you want to be able to edit
git rebase -i HEAD~n

Once you squash your commits - choose the e/rfor editing the message:

压缩提交后 - 选择e/r用于编辑消息:

Enter image description here

在此处输入图片说明

Important note about interactive rebase

关于交互式 rebase 的重要说明

When you use git rebase -i HEAD~nthere can be morethan n commits. Git will "collect" all the commits in the last n commits, and if there was a merge somewhere in between that range you will see all the commits as well, so the outcome will be n + .

当你使用git rebase -i HEAD~n可以有更多的比的n条提交。Git 将“收集”最近 n 次提交中的所有提交,如果在该范围之间的某处存在合并,您也会看到所有提交,因此结果将为 n + 。

Good tip:

好提示:

If you have to do it for more than a single branch and you might face conflicts when amending the content, set up git rerereand let Git resolve those conflicts automatically for you.

如果您必须为多个分支执行此操作,并且在修改内容时可能会遇到冲突,请设置git rerere并让 Git 自动为您解决这些冲突。



Documentation

文档

回答by lfx_cool

git commit --amend -m "your new message"

回答by Aristotle Pagaltzis

If the commit you want to fix isn't the most recent one:

如果您要修复的提交不是最新的提交:

  1. git rebase --interactive $parent_of_flawed_commit

    If you want to fix several flawed commits, pass the parent of the oldest one of them.

  2. An editor will come up, with a list of all commits since the one you gave.

    1. Change pickto reword(or on old versions of Git, to edit) in front of any commits you want to fix.
    2. Once you save, Git will replay the listed commits.

  3. For each commit you want to reword, Git will drop you back into your editor. For each commit you want to edit, Git drops you into the shell. If you're in the shell:

    1. Change the commit in any way you like.
    2. git commit --amend
    3. git rebase --continue
  1. git rebase --interactive $parent_of_flawed_commit

    如果您想修复多个有缺陷的提交,请传递其中最旧的提交的父项。

  2. 编辑器会出现,列出自您提交以来的所有提交。

    1. 在要修复的任何提交之前更改pickreword(或在旧版本的 Git 上更改为edit)。
    2. 保存后,Git 将重放列出的提交。

  3. 对于您想要改写的每个提交,Git 会将您放回编辑器。对于您要编辑的每个提交,Git 会将您放入 shell。如果您在 shell 中:

    1. 以您喜欢的任何方式更改提交。
    2. git commit --amend
    3. git rebase --continue

Most of this sequence will be explained to you by the output of the various commands as you go. It's very easy; you don't need to memorise it – just remember that git rebase --interactivelets you correct commits no matter how long ago they were.

这个序列的大部分内容将通过各种命令的输出向您解释。这很容易;你不需要记住它——只要记住它git rebase --interactive可以让你更正提交,无论它们是多久以前的。



Note that you will not want to change commits that you have already pushed. Or maybe you do, but in that case you will have to take great care to communicate with everyone who may have pulled your commits and done work on top of them. How do I recover/resynchronise after someone pushes a rebase or a reset to a published branch?

请注意,您不会想要更改已经推送的提交。或者也许你会这样做,但在这种情况下,你必须非常小心地与可能已经撤回你的提交并在它们之上完成工作的每个人进行交流。在有人将 rebase 或重置推送到已发布的分支后,如何恢复/重新同步?

回答by Fatih Acet

To amend the previous commit, make the changes you want and stage those changes, and then run

要修改先前的提交,请进行所需的更改并暂存这些更改,然后运行

git commit --amend

This will open a file in your text editor representing your new commit message. It starts out populated with the text from your old commit message. Change the commit message as you want, then save the file and quit your editor to finish.

这将在您的文本编辑器中打开一个文件,代表您的新提交消息。它开始填充来自旧提交消息的文本。根据需要更改提交消息,然后保存文件并退出编辑器以完成。

To amend the previous commit and keep the same log message, run

要修改先前的提交并保留相同的日志消息,请运行

git commit --amend -C HEAD

To fix the previous commit by removing it entirely, run

要通过完全删除它来修复以前的提交,请运行

git reset --hard HEAD^

If you want to edit more than one commit message, run

如果要编辑多个提交消息,请运行

git rebase -i HEAD~commit_count

(Replace commit_countwith number of commits that you want to edit.) This command launches your editor. Mark the first commit (the one that you want to change) as “edit” instead of “pick”, then save and exit your editor. Make the change you want to commit and then run

(将commit_count替换为您要编辑的提交数。)此命令启动您的编辑器。将第一个提交(您要更改的提交)标记为“edit”而不是“pick”,然后保存并退出编辑器。进行您要提交的更改,然后运行

git commit --amend
git rebase --continue

Note: You can also "Make the change you want" from the editor opened by git commit --amend

注意:您还可以从打开的编辑器中“进行您想要的更改” git commit --amend

回答by John

As already mentioned, git commit --amendis the way to overwrite the last commit. One note: if you would like to also overwrite the files, the command would be

如前所述,git commit --amend是覆盖上次提交的方法。一个注意事项:如果您还想覆盖文件,则命令为

git commit -a --amend -m "My new commit message"

回答by Mark

You also can use git filter-branchfor that.

你也可以使用git filter-branch它。

git filter-branch -f --msg-filter "sed 's/errror/error/'" $flawed_commit..HEAD

It's not as easy as a trivial git commit --amend, but it's especially useful, if you already have some merges after your erroneous commit message.

这并不像一个微不足道的git commit --amend那么简单,但它特别有用,如果您在错误的提交消息之后已经进行了一些合并。

Note that this will try to rewrite everycommit between HEADand the flawed commit, so you should choose your msg-filtercommand very wisely ;-)

请注意,这将尝试重写有缺陷的提交之间的每个HEAD提交,因此您应该msg-filter非常明智地选择您的命令;-)

回答by krevedko

I prefer this way:

我更喜欢这种方式:

git commit --amend -c <commit ID>

Otherwise, there will be a new commit with a new commit ID.

否则,将有一个带有新提交 ID 的新提交。

回答by Akhilraj N S

If you are using the Git GUI tool, there is a button named Amend last commit. Click on that button and then it will display your last commit files and message. Just edit that message, and you can commit it with a new commit message.

如果您使用的是 Git GUI 工具,则有一个名为Amend last commit的按钮。单击该按钮,然后它将显示您上次提交的文件和消息。只需编辑该消息,您就可以使用新的提交消息提交它。

Or use this command from a console/terminal:

或者从控制台/终端使用此命令:

git commit -a --amend -m "My new commit message"

回答by Shoaib Ud-Din

You can use Git rebasing. For example, if you want to modify back to commit bbc643cd, run

您可以使用Git 变基。例如,如果要修改回提交 bbc643cd,则运行

$ git rebase bbc643cd^ --interactive

In the default editor, modify 'pick' to 'edit' in the line whose commit you want to modify. Make your changes and then stage them with

在默认编辑器中,将要修改其提交的行中的“pick”修改为“edit”。进行更改,然后使用

$ git add <filepattern>

Now you can use

现在你可以使用

$ git commit --amend

to modify the commit, and after that

修改提交,然后

$ git rebase --continue

to return back to the previous head commit.

返回到前一个头部提交。

回答by Heena Hussain

  1. If you only want to modify your last commit message, then do:

    git commit --amend
    
  1. 如果您只想修改上次提交消息,请执行以下操作:

    git commit --amend
    

That will drop you into your text editor and let you change the last commit message.

这将使您进入文本编辑器并让您更改最后的提交消息。

  1. If you want to change the last three commit messages, or any of the commit messages up to that point, supply HEAD~3to the git rebase -icommand:

    git rebase -i HEAD~3
    
  1. 如果要更改最后三个提交消息,或到那时为止的任何提交消息,请提供HEAD~3以下git rebase -i命令:

    git rebase -i HEAD~3