代码之家  ›  专栏  ›  技术社区  ›  13ren

在Git中编辑根提交?

  •  279
  • 13ren  · 技术社区  · 16 年前

    有几种方法可以从以后的提交更改消息:

    git commit --amend                    # for the most recent commit
    git rebase --interactive master~2     # but requires *parent*
    

    如何更改第一次提交(没有父级)的提交消息?

    5 回复  |  直到 11 年前
        1
  •  308
  •   Zombo tliff    11 年前

    假设您有一个干净的工作树,您可以执行以下操作。

    # checkout the root commit
    git checkout <sha1-of-root>
    
    # amend the commit
    git commit --amend
    
    # rebase all the other commits in master onto the amended root
    git rebase --onto HEAD HEAD master
    
        2
  •  616
  •   ParaBolt ecdpalma    6 年前

    从Git版本开始 1.7.12 ,您现在可以使用

    git rebase -i --root
    

    Documentation

        3
  •  72
  •   Community Mohan Dere    9 年前

    ecdpalma's answer --root 选择告诉 rebase 您要重写根/第一次提交:

    git rebase --interactive --root
    

    reword <root commit sha> <original message>
    pick <other commit sha> <message>
    ...
    

    这就是我们的解释 --根 the Git rebase docs (我的重点):

    <branch> ,而不是使用 <upstream> 这允许您在分支上重新设置根提交的基础 .

        4
  •  14
  •   jakub.g    7 年前

    只是为了提供更高评分答案的替代方案:

    git commit --allow-empty -m "Initial commit"
    

    然后才开始做“真正的”提交。然后,您可以轻松地在提交的基础上重新设置基础,例如,使用标准方式 git rebase -i HEAD^

        5
  •  4
  •   Alexander Groß    12 年前

    你可以用 git filter-branch

    cd test
    git init
    
    touch initial
    git add -A
    git commit -m "Initial commit"
    
    touch a
    git add -A
    git commit -m "a"
    
    touch b
    git add -A
    git commit -m "b"
    
    git log
    
    -->
    8e6b49e... b
    945e92a... a
    72fc158... Initial commit
    
    git filter-branch --msg-filter \
    "sed \"s|^Initial commit|New initial commit|g\"" -- --all
    
    git log
    -->
    c5988ea... b
    e0331fd... a
    51995f1... New initial commit
    
    推荐文章