如何修改Git中的特定提交消息?

问题描述 投票:0回答:5

我正在尝试更改 SourceTree 中的提交消息,但找不到该选项在哪里。还没推呢

如何修改 SourceTree 或命令行中旧提交的消息?

git atlassian-sourcetree
5个回答
9
投票

没有任何功能可以做到这一点,因为 git 内部是如何工作的,sha1 密封了每个提交。

但是你可以:

  • 如果该消息是最后一次提交的消息,则进行“修改”。

  • 执行

    git rebase -i
    也称为 rebase 交互,并为要重写提交消息的每个提交选择“reword”(或“r”)。

  • 使用 git 'notes' 在现有评论旁边加入新评论(但处理它并不简单,因为你必须明确地推送注释并查询它们才能看到它们......)


3
投票

假设分支是

A-B-C-D-E
,你想修改C。这是我更喜欢的一种解决方案:

git reset C --hard
#do some changes and add
git commit --amend
git cherry-pick C..E
#or git cherry-pick D E

2
投票

步骤

请按照以下步骤编辑之前提交的提交消息

步骤1

在命令行上,导航到包含要修改的提交的存储库。

使用

git rebase -i HEAD~n
命令在默认文本编辑器中显示最后 n 次提交的列表。

例如

# Displays a list of the last 3 commits on the current branch
$ git rebase -i HEAD~3

步骤2

该列表将类似于以下内容:

pick e499d89 Delete CNAME
pick 0c39034 Better README
pick f7fde4a Change the commit message but push the same commit.

# Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

步骤3

在要更改的每个提交消息之前将

pick
替换为
reword

注意:如果您使用 VIM,则按

i
即可编辑文件。然后继续将
pick
替换为
reword
,并在更改完成后按
ESC

步骤4

保存并关闭提交列表文件。 (使用

:wq
保存并退出vim)

步骤 5

在每个生成的提交文件中,键入新的提交消息,保存文件并关闭它。

步骤6

当您准备好将更改推送到 GitHub 时,请使用 push --force 命令强制推送旧的提交。

$ git push --force origin EXAMPLE-BRANCH

参考

  1. 如何编辑旧提交的消息

1
投票
  1. 使用
    git reflog
    查找该提交,然后找到其 sha-id
  2. 要转到该提交,请使用

    git reset --hard sha-id
    (如果您不想保留当前状态的更改)

    或者,

    使用

    git reset --soft sha-id
    (如果您想保留更改)

  3. 现在进行提交——修改提交......

  4. 现在,检查您要修改的提交是否已被推送,

    如果,,则进行变基并推送...

       `git push ` and revert back your head to where it was earlier using its sha-id
    

    如果您不执行上述步骤,您的分支将与远程分支分开,您将在

    git status

    中看到这一点

    否则,只需使用其 sha-id 恢复到您的提交


0
投票

以下步骤帮助我们修改现有的提交消息。

步骤 1. 输入 git rebase -i HEAD~N,其中 N 是执行变基操作的提交次数。例如,如果您想更改第四个和第五个最新提交,您可以输入:

git rebase -i HEAD~5

第 2 步。该命令将在默认文本编辑器中显示最新的 X 提交:

pick 43f8707f9 fix: update dependency json5 to ^2.1.1
pick cea1fb88a fix: update dependency verdaccio to ^4.3.3
pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2
pick c5e078656 chore: update dependency flow-bin to ^0.109.0
pick 11ce0ab34 fix: Fix spelling.

#Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 commands)

步骤 3. 移至要更改的提交消息行并用 reword 替换 pick:

reword 43f8707f9 fix: update dependency json5 to ^2.1.1
reword cea1fb88a fix: update dependency verdaccio to ^4.3.3
pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2
pick c5e078656 chore: update dependency flow-bin to ^0.109.0
pick 11ce0ab34 fix: Fix spelling.

# Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 commands)

第 4 步:保存更改并关闭编辑器。

第 5 步:对于每个选定的提交,都会打开一个新的文本编辑器窗口。更改提交消息,保存文件,然后关闭编辑器。

fix: update dependency json5 to ^2.1.1

第6步:将更改强制推送到远程存储库:

git push --force <remoteName> <branchName>
© www.soinside.com 2019 - 2024. All rights reserved.