拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何修改 Git 提交消息

如何修改 Git 提交消息

白鹭 - 2022-07-05 2161 0 2

一、概述

在本教程中,我们将了解如何修改 Git 提交消息,无论它是最近的提交还是较旧的提交。

2. 修改最近的提交信息

我们将从最简单的情况开始。 让我们构建一个在其提交消息中有错字的琐碎提交:

$ 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

现在让我们确认我们最新的提交消息中是否存在拼写错误,并注意提交的哈希:

$ git log -1
commit 3e9ac2dbcdde562e50c5064b288f5b3fa23f39da (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <[email protected]>
Date: Tue Jun 21 21:53:12 2022 +0200

Ading file1

要恢复错字,我们将使用amend选项:

$ 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

同样,让我们显示最近的提交:

$ git log -1
commit 66dfa067969f941eef5304a6fbcd5b22d0ba6c2b (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <[email protected]>
Date: Tue Jun 21 21:53:12 2022 +0200

Adding file1

我们现在可以确认最新消息中的拼写错误已修复,但也请注意提交哈希已更改。 从技术上讲,我们没有更改我们的提交,而是用一个新的提交替换了它。

3. 改写旧的提交消息

现在让我们添加两个新的提交,这样拼写错误就不会出现在最近的提交中,而是出现在较旧的提交中:

$ 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

讓我們檢查一下我們剛剛添加的兩個提交:

$ git log -2
commit 517193e1e99c784efd48086f955fcdbc3110d097 (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <[email protected]>
Date: Tue Jun 21 22:04:56 2022 +0200

Adding file3

commit ffb7a68bf63c7da9bd0b261ebb9b2ca548aa1333
Author: baeldung <[email protected]>
Date: Tue Jun 21 22:02:59 2022 +0200

Ading file2

Git 的amend選項只適用於最新的 commit ,所以這次我們不能用它來修復錯字。

相反,我們將使用 。rebase

3.1。 启动交互式变基

要修复较旧的提交消息,让我们通过运行以下命令继续所谓的交互式 rebase :

$ git rebase -i HEAD~2
hint: Waiting for your editor to close the file...

这里的意味着我们将重新访问最近的两个提交。HEAD~2

这将在您的机器上打开和 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

正如我们所看到的,我们可以在 rebase 提交时做各种各样的事情,比如通过命令将两个提交合并为一个。squash

在这里,我们要重写提交消息,所以我们将使用reword命令。

3.2.改写提交消息

编辑器中的前两行包含以下文本:

pick ffb7a68bf6 Ading file2
pick 517193e1e9 Adding file3

请注意,在此视图中,提交是从最旧到最新列出的,而不是我们使用时。[git log](https://git-scm.com/docs/git-log)

让我们将第一行改为使用命令而不是命令; 我们将第二次提交,因为我们希望保持该消息原样:rewordpickpick

reword ffb7a68bf6 Ading file2
pick 517193e1e9 Adding file3

如果我们想在一个 rebase 中更改两条消息,我们可以简单地将两行的命令更改为 。reword

现在,我们还没有更改提交消息。 所以让我们保存我们的文档并关闭文本编辑器,它让 Git 知道我们已经完成了我们的变基指令。

Git 现在将处理变基命令,在需要我们交互时提示我们。 由于我们告诉 Git 第一次提交,它会用第一次提交的内容重新打开文本编辑器。 第一行包含提交消息:reword

Ading file2

以下几行的注释描述了操作的工作方式。reword

让我们通过将第一行修改为“Adding file2”、保存文件并关闭编辑器来编辑提交消息。

Git 将更新我们的提交消息,然后完成其余的变基指令。 让我们确认 Git 的工作:

$ git log -2
commit 421d446d77d4824360b516e2f274a7c5299d6498 (HEAD -> articles/BAEL-5627-how-to-modify-git-commit-message)
Author: baeldung <[email protected]>
Date: Tue Jun 21 22:04:56 2022 +0200

Adding file3

commit a6624ee55fdb9a6a2446fbe6c6fb8fe3bc4bd456
Author: baeldung <[email protected]>
Date: Tue Jun 21 22:02:59 2022 +0200

Adding file2

另外,请注意,这些提交的提交哈希已更改,就像我们之前修改过的提交一样。 我们所有的原始提交都已替换为新提交。

4. 推送你重写的提交

此时,我们的分支上有三个提交:一个修改后的提交和两个重新定位的提交。 它们都具有与我们原始提交不同的提交哈希值。

只要原始提交没有被推送到任何远程仓库,我们就可以正常推送

但是,假设我们之前在替换之前推送了任何有缺陷的提交。 在这种情况下,远程存储库将拒绝我们的新推送,因为我们的本地提交历史不再与存储库的历史兼容。

因此,如果我们想要推送更正(但之前推送)的提交,我们将不得不使用force选项:

$ git push --force

这个命令应该小心使用,因为它会用我们的更改覆盖远程分支,如果分支被其他人使用,这可能会导致问题。

5.结论

在本文中,我们了解了如何编辑提交消息,无论是最后一条还是较旧的一条。 我们还看到了如何将更改的提交推送到具有原始提交的存储库,并指出这应该小心完成。


标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *