如何显示使用“git rebase -i”修改的提交消息中的差异?

Rob*_*ark 5 git git-diff git-rebase git-commit

我做了一个git rebase -i并重写了一次提交。

如何显示我提交的新提交消息和旧提交消息之间的差异?(例如,来自 中的提交git reflog

Von*_*onC 4

您首先需要使用 找到相关的提交哈希值git reflog
例如:

git reflog

a123b4c (HEAD -> main) HEAD@{0}: rebase -i (finish): returning to refs/heads/main
a123b4c (HEAD -> main) HEAD@{1}: rebase -i (pick): commit message 1
c456d7e HEAD@{2}: rebase -i (start): checkout HEAD~2
e890f1g HEAD@{3}: commit: old commit message 2
Run Code Online (Sandbox Code Playgroud)

在这个例子中:

  • a123b4c是新提交的哈希值(带有改写消息的提交),并且
  • e890f1g是原始提交的哈希值(包含旧消息的提交)。

现在,您可以使用git show --pretty=format:%s <commit-hash>命令显示提交消息,并替换<commit-hash>为您在上一步中找到的哈希值:

git show --pretty=format:%s a123b4c
commit message 1

git show --pretty=format:%s e890f1g
old commit message 2
Run Code Online (Sandbox Code Playgroud)

这将为您提供相应哈希值的提交消息,然后您可以手动比较这些消息。


您可以编写一个 bash 函数来使用该diff命令比较两个提交消息。
该函数需要两个参数:您要比较的两个提交的哈希值。

diff_git_commit_messages() {
    if [ $# -ne 2 ]; then
        echo "Usage: ${FUNCNAME[0]} <commit-hash-1> <commit-hash-2>"
        return
    fi
    diff \
        <(git show --pretty=format:%s $1) \
        <(git show --pretty=format:%s $2)
}
Run Code Online (Sandbox Code Playgroud)

例如,

$ diff_git_commit_messages 6bfa911 HEAD
1c1
< add file
---
> my new msg
Run Code Online (Sandbox Code Playgroud)