1. Overview
1.概述
In this tutorial, we’ll see how to modify a Git commit message, whether it’s the most recent commit or an older one.
在本教程中,我们将看到如何修改Git提交信息,无论是最近的提交还是较早的提交。
2. Amend the Most Recent Commit Message
2.修改最近的承诺信息
We’ll start with the easiest case. Let’s build a trivial commit that has a typo in its commit message:
我们从最简单的情况开始。让我们建立一个微不足道的提交,它的提交信息中有一个错字。
$ touch file1
$ git add file1
$ git commit -m "Ading file1"
[articles/BAEL-5627-how-to-modify-git-commit-message 3e9ac2dbcd] Ading file1
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1
Let’s now confirm the typo exists in our latest commit message, and also note the commit’s hash:
现在让我们确认一下最新的提交信息中存在的错别字,同时注意一下提交的哈希值。
$ git log -1
commit 3e9ac2dbcdde562e50c5064b288f5b3fa23f39da (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <baeldung@baeldung.com>
Date: Tue Jun 21 21:53:12 2022 +0200
Ading file1
To fix the typo, we’ll use the amend option:
为了修改错别字,我们将使用修改选项:。
$ git commit --amend -m "Adding file1"
[articles/BAEL-5627-how-to-modify-git-commit-message 66dfa06796] Adding file1
Date: Tue Jun 21 21:53:12 2022 +0200
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1
Again, let’s display the most recent commit:
再一次,让我们显示最近的提交。
$ git log -1
commit 66dfa067969f941eef5304a6fbcd5b22d0ba6c2b (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <baeldung@baeldung.com>
Date: Tue Jun 21 21:53:12 2022 +0200
Adding file1
We can now confirm that the typo is fixed in the most recent message, but also note that the commit hash has changed. Technically, we have not changed our commit but replaced it with a new one.
我们现在可以确认,在最近的信息中,这个错别字已经被修正了,但也注意到,提交的哈希值已经改变了。从技术上讲,我们并没有改变我们的提交,而是用一个新的提交替换了它。
3. Reword an Older Commit Message
3.重写一个旧的承诺信息
Let’s now add two new commits so that the typo does not exist in the most recent commit but in an older one:
现在让我们添加两个新的提交,这样,错别字就不会存在于最近的提交中,而是存在于较早的提交中。
$ touch file2
$ git add file2
$ git commit -m "Ading file2"
[articles/BAEL-5627-how-to-modify-git-commit-message ffb7a68bf6] Ading file2
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file2
$ touch file3
$ git add file3
$ git commit -m "Adding file3"
[articles/BAEL-5627-how-to-modify-git-commit-message 517193e1e9] Adding file3
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 fil3
Let’s check the two commits we just added:
让我们检查一下我们刚刚添加的两个提交。
$ git log -2
commit 517193e1e99c784efd48086f955fcdbc3110d097 (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <baeldung@baeldung.com>
Date: Tue Jun 21 22:04:56 2022 +0200
Adding file3
commit ffb7a68bf63c7da9bd0b261ebb9b2ca548aa1333
Author: baeldung <baeldung@baeldung.com>
Date: Tue Jun 21 22:02:59 2022 +0200
Ading file2
Git’s amend option only works with the latest commit, so we can’t use that to fix the typo this time.
Git的amend选项只适用于最新的提交,所以这次我们不能用它来修复这个错误。
Instead, we’ll use rebase.
相反,我们将使用rebase。
3.1. Start an Interactive Rebase
3.1.启动交互式重构
To fix the older commit message, let’s proceed with what is called an interactive rebase by running the following command:
为了修复旧的提交信息,让我们通过运行以下命令来进行所谓的交互式重定位。
$ git rebase -i HEAD~2
hint: Waiting for your editor to close the file...
HEAD~2 here means that we’ll be revisiting the two most recent commits.
HEAD~2这里意味着我们将重新审视最近的两个提交。
This will open the text editor associated with Git on your machine and populate the editor with all the commands that can be used during a rebase procedure, including:
这将打开你机器上与Git相关的文本编辑器,并在编辑器中填入所有可以在重建程序中使用的命令,包括。
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
As we can see, we can do all sorts of things when rebasing commits, like collapsing two commits into one via the squash command.
正如我们所看到的,我们可以在重定位提交时做各种事情,比如通过squash命令将两个提交折叠成一个。
Here, we want to rewrite the commit message, so we’ll use the reword command.
这里,我们想重写提交信息,所以我们将使用reword命令。
3.2. Reword the Commit Message
3.2.重写承诺信息
The two first lines in the editor contain the following text:
编辑器中的两个第一行包含以下文字。
pick ffb7a68bf6 Ading file2
pick 517193e1e9 Adding file3
Note that, in this view, the commits are listed from the oldest to the most recent, as opposed to when we use git log.
请注意,在这个视图中,提交的内容是从最早的到最近的,与我们使用git log不同。
Let’s change the first line to use the reword command instead of the pick command; we’ll leave pick for the second commit because we want to keep that message as it is:
让我们把第一行改为使用reword命令,而不是pick命令;我们把pick留给第二次提交,因为我们想保持该信息的原貌。
reword ffb7a68bf6 Ading file2
pick 517193e1e9 Adding file3
If we wanted to change both messages in a single rebase, we could simply change the commands on both lines to reword.
如果我们想在一次重建中改变这两条信息,我们可以简单地将两行的命令改为reword。
Now, we haven’t changed the commit message just yet. So let’s save our file and close the text editor, which lets Git know we have finished our rebasing instructions.
现在,我们还没有改变提交信息。所以让我们保存我们的文件并关闭文本编辑器,这让Git知道我们已经完成了重定向指令。
Git will now process the rebase commands, prompting us when it needs our interaction. Since we told Git to reword the first commit, it’ll reopen the text editor with the contents of the first commit. The first line contains the commit message:
Git 现在将处理 rebase 命令,在需要我们互动时提示我们。由于我们告诉 Git 要给第一次提交的内容写上reword,它将用第一次提交的内容重新打开文本编辑器。第一行包含提交信息。
Ading file2
The comments on the following lines describe how the reword operation will work.
下面几行的注释描述了reword操作将如何进行。
Let’s edit the commit message by modifying the first line to “Adding file2”, saving the file, and closing the editor.
让我们编辑提交信息,将第一行修改为 “添加file2″,保存文件,并关闭编辑器。
Git will update our commit message and then finish with the rest of the rebase instructions. Let’s confirm Git’s work:
Git会更新我们的提交信息,然后完成其余的重构指令。让我们确认一下Git的工作。
$ git log -2
commit 421d446d77d4824360b516e2f274a7c5299d6498 (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <baeldung@baeldung.com>
Date: Tue Jun 21 22:04:56 2022 +0200
Adding file3
commit a6624ee55fdb9a6a2446fbe6c6fb8fe3bc4bd456
Author: baeldung <baeldung@baeldung.com>
Date: Tue Jun 21 22:02:59 2022 +0200
Adding file2
Also, note that the commit hashes of these commits have changed, just like our amended commit before. All of our original commits have been replaced with new ones.
另外,请注意,这些提交的哈希值已经改变了,就像我们之前修正的提交一样。我们所有的原始提交已经被新的提交所取代。
4. Pushing Your Rewritten Commits
4.推送你的重写承诺
At this point, we have three commits on our branch: an amended one and two rebased ones. They all have different commit hashes from our original commits.
这时,我们的分支上有三个提交:一个修正的提交和两个重构的提交。它们的提交哈希值都与我们的原始提交不同。
As long as the original commits haven’t been pushed to any remote repository, we’ll be able to push normally.
只要原始提交还没有被推送到任何远程仓库,我们就可以正常地推送。
However, suppose we had previously pushed any of our flawed commits before replacing them. In that case, the remote repository will refuse our new push because our local commit history is no longer compatible with the repository’s history.
然而,假设我们在替换之前已经推送了任何有缺陷的提交。在这种情况下,远程版本库会拒绝我们的新推送,因为我们的本地提交历史已经与版本库的历史不兼容了。
Thus, if we want to push corrected (but previously pushed) commits, we’ll have to use the force option:
因此,如果我们想推送修正过的(但以前推送过的)提交,我们就必须使用force选项:。
$ git push --force
This command should be used carefully because it will overwrite the remote branch with our changes, which can cause problems if the branch is being used by other people.
这个命令要小心使用,因为它会用我们的修改覆盖远程分支,如果该分支被其他人使用,就会造成问题。
5. Conclusion
5.总结
In this article, we’ve seen how to edit a commit message, be it the last one or an older one. We also saw how to push a changed commit to a repository that has the original one, noting that this should be done with care.
在这篇文章中,我们看到了如何编辑一个提交信息,不管是最后一个还是更早的一个。我们还看到了如何将修改后的提交推送到拥有原始提交的版本库中,但需要注意的是,这样做要谨慎。