给定以下消息格式,如何重新提交任何提交的消息? [英] How to reword any commit's message given the following message format?

查看:148
本文介绍了给定以下消息格式,如何重新提交任何提交的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在没有更改任何其他提交信息的情况下,根据给定的提交ID修改提交的消息.但是,提交消息应接受换行符,类似于使用git commit命令进行换行.

例如,考虑以下提交

commit <id>
Author: <user-name> <user-email.com>
Date:   ...

    Hello World

我想将提交消息改写为此

    Hello World

    Text after line break1
    More text

通常的方法是交互式地进行变基,然后使用git commit --amend编辑提交或对该提交执行重新编写字词的操作.但是,这将修改提交信息,例如提交者的电子邮件,时间等.

(检查更新部分是否变基)

git的过滤分支将仅通过更改提交消息和ID即可重写提交,如此 answer .

但是,如何使用filter-branch以上述提交消息格式重写提交?

更新:

这是交互式变基的示例.

  • 使用2次提交创建测试存储库

    test@ubuntu:~/temp_git$ git init
    Initialized empty Git repository in /home/test/temp_git/.git/
    test@ubuntu:~/temp_git$ touch file1
    test@ubuntu:~/temp_git$ git add .
    test@ubuntu:~/temp_git$ git -c "user.name=A" -c "user.email=a@xyz.com" commit -am "Add File1" --author="B <b@xyz.com>"
    [master (root-commit) f122a34] Add File1
     Author: B <b@xyz.com>
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 file1
    test@ubuntu:~/temp_git$ touch file2
    test@ubuntu:~/temp_git$ git add .
    test@ubuntu:~/temp_git$ git -c "user.name=B" -c "user.email=b@xyz.com" commit -am "Add File2" --author="B <b@xyz.com>"
    [master 3b023cf] Add File2
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 file2
    

  • 上面创建的提交具有不同的作者和提交者.完整的日志如下:

    test@ubuntu:~/temp_git$ git log --format="fuller"
    commit 3b023cf256ae3498fbaf740329d94842143a5e4a (HEAD -> master)
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:28:07 2019 +0530
    Commit:     B <b@xyz.com>
    CommitDate: Tue Oct 15 08:28:07 2019 +0530
    
        Add File2
    
    commit f122a341e31691f3170207c9a452ff18846fe120
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:27:22 2019 +0530
    Commit:     A <a@xyz.com>
    CommitDate: Tue Oct 15 08:27:22 2019 +0530
    
        Add File1
    

  • 以用户A的身份执行交互式rebase并重新措辞root提交

    test@ubuntu:~/temp_git$ git -c "user.name=A" -c "user.email=a@xyz.com" rebase -i --root
    [detached HEAD 228b423] Add File1 (test)
     Author: B <b@xyz.com>
     Date: Tue Oct 15 08:27:22 2019 +0530
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 file1
    Successfully rebased and updated refs/heads/master.
    

  • 已更新日志,其中包含提交者信息的更改

    test@ubuntu:~/temp_git$ git log --format="fuller"
    commit 0d5e6a5fed5b22fc5f8e310c6e98b1e6b8a821b8 (HEAD -> master)
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:28:07 2019 +0530
    Commit:     A <a@xyz.com>
    CommitDate: Tue Oct 15 08:31:41 2019 +0530
    
        Add File2
    
    commit 228b423e3e511c5954823e42df51a6f6acae91cf
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:27:22 2019 +0530
    Commit:     A <a@xyz.com>
    CommitDate: Tue Oct 15 08:31:28 2019 +0530
    
        Add File1 (test)
    

解决方案

如在此处回答的那样,可以使用以下脚本用于使用filter分支对提交进行改写:

#! /bin/bash
REV=$1
MESSAGE=$2
FILTER="test $(echo '$GIT_COMMIT') = $(git rev-parse $REV) && echo $MESSAGE || cat"
git filter-branch --msg-filter "$FILTER" -- --all

usage: ./script_name.sh <commit-id> "commit message"

但是要在新的提交消息中添加换行符,我必须将提交消息修改为此:"$(echo -e 'summary_line\n\nmessage\nmore_message')"在此处回答. 根据 commits .

此外,由于注释此处中提到的转义序列字符,不得不修改脚本.: 此行$(git rev-parse $REV) && echo $MESSAGE中的

$MESSAGE更改为\"$MESSAGE\"

如果脚本名称为reword-commit.sh,则是最后一条命令(将在git存储库中运行)

./reword-commit.sh <commit-id> "$(echo -e 'Hello World\n\nText after line break1\nMore Text')"

I need to modify a commit's message given the commit id without changing any other commit info. However, the commit message should accept line breaks,etc similar to how it is done using the git commit command.

For example, consider the following commit

commit <id>
Author: <user-name> <user-email.com>
Date:   ...

    Hello World

I want to reword the commit message to this

    Hello World

    Text after line break1
    More text

The normal method would be to rebase interactively, and then edit the commit using git commit --amend or perform a reword operation on that commit. However, this will modify commit info such as committer email, time etc.

(Check the update section for rebase)

Filter-branch in git will allow to rewrite the commit by only changing the commit message and ids as shown in this answer.

But, how do I reword a commit with the above commit message format using filter-branch ?

Update:

Here's an example for interactive rebase.

  • Create a test repository with 2 commits

    test@ubuntu:~/temp_git$ git init
    Initialized empty Git repository in /home/test/temp_git/.git/
    test@ubuntu:~/temp_git$ touch file1
    test@ubuntu:~/temp_git$ git add .
    test@ubuntu:~/temp_git$ git -c "user.name=A" -c "user.email=a@xyz.com" commit -am "Add File1" --author="B <b@xyz.com>"
    [master (root-commit) f122a34] Add File1
     Author: B <b@xyz.com>
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 file1
    test@ubuntu:~/temp_git$ touch file2
    test@ubuntu:~/temp_git$ git add .
    test@ubuntu:~/temp_git$ git -c "user.name=B" -c "user.email=b@xyz.com" commit -am "Add File2" --author="B <b@xyz.com>"
    [master 3b023cf] Add File2
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 file2
    

  • Commits created above have different author and committer. The complete log is as follows :

    test@ubuntu:~/temp_git$ git log --format="fuller"
    commit 3b023cf256ae3498fbaf740329d94842143a5e4a (HEAD -> master)
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:28:07 2019 +0530
    Commit:     B <b@xyz.com>
    CommitDate: Tue Oct 15 08:28:07 2019 +0530
    
        Add File2
    
    commit f122a341e31691f3170207c9a452ff18846fe120
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:27:22 2019 +0530
    Commit:     A <a@xyz.com>
    CommitDate: Tue Oct 15 08:27:22 2019 +0530
    
        Add File1
    

  • Performing interactive rebase as user A and rewording the root commit

    test@ubuntu:~/temp_git$ git -c "user.name=A" -c "user.email=a@xyz.com" rebase -i --root
    [detached HEAD 228b423] Add File1 (test)
     Author: B <b@xyz.com>
     Date: Tue Oct 15 08:27:22 2019 +0530
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 file1
    Successfully rebased and updated refs/heads/master.
    

  • Updated log with the changes in committer info

    test@ubuntu:~/temp_git$ git log --format="fuller"
    commit 0d5e6a5fed5b22fc5f8e310c6e98b1e6b8a821b8 (HEAD -> master)
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:28:07 2019 +0530
    Commit:     A <a@xyz.com>
    CommitDate: Tue Oct 15 08:31:41 2019 +0530
    
        Add File2
    
    commit 228b423e3e511c5954823e42df51a6f6acae91cf
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:27:22 2019 +0530
    Commit:     A <a@xyz.com>
    CommitDate: Tue Oct 15 08:31:28 2019 +0530
    
        Add File1 (test)
    

解决方案

As answered here, the following script can be used to reword the commit using filter branch:

#! /bin/bash
REV=$1
MESSAGE=$2
FILTER="test $(echo '$GIT_COMMIT') = $(git rev-parse $REV) && echo $MESSAGE || cat"
git filter-branch --msg-filter "$FILTER" -- --all

usage: ./script_name.sh <commit-id> "commit message"

However to add line breaks to the new commit message, I had to modify the commit message to this: "$(echo -e 'summary_line\n\nmessage\nmore_message')" as answered here. The above command adds two line breaks after the summary_line as per the general convention for commits.

Furthermore, the script had to be modified due to the escape sequence characters as mentioned in the comments here:

$MESSAGE in this line $(git rev-parse $REV) && echo $MESSAGE is changed to \"$MESSAGE\"

So the final command if the script name were reword-commit.sh, (to be run inside the git repository)

./reword-commit.sh <commit-id> "$(echo -e 'Hello World\n\nText after line break1\nMore Text')"

这篇关于给定以下消息格式,如何重新提交任何提交的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆