【问题标题】:Edit the root commit in Git?在 Git 中编辑根提交?
【发布时间】:2011-01-08 08:02:36
【问题描述】:

有一些方法可以从以后的提交中更改消息:

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

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

【问题讨论】:

标签: git git-rebase git-commit git-rewrite-history git-amend


【解决方案1】:

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

git rebase -i --root

Documentation

【讨论】:

  • 是否可以使用此命令重新设置所有分支的根?似乎这会将当前分支分离到新根上,而所有其他分支将保留在旧根上
  • @woojoo666 你将不得不将分支重新定位到新的根目录。像往常一样。
  • @Atcold 如果没有上游根则不起作用
  • FWIW:我也希望将其视为公认的答案,特别是因为它与我一直以来最喜欢的 git 命令相匹配,用于在开发的早期阶段清理新项目的历史,即:git rebase --interactive --autosquash --autostash --root
  • @Leo 你的评论是什么意思?我看不到第一部分和第二部分之间的联系 - 需要一段时间与它有什么关系?
【解决方案2】:

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

# 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

【讨论】:

  • 我认为应该是git rebase --onto HEAD &lt;sha1-of-root&gt; master
  • 对,但是您想要 git rebaseoriginal 根提交。 git rebase (master) 中应用不在 中的提交; HEAD 不在master 中,因此您的版本会尝试应用所有master
  • 是的,确保它是git rebase --onto HEAD &lt;sha1-of-root&gt; master,其中&lt;sha1-of-root&gt;git checkout &lt;sha1-of-root&gt; 中使用的相同。否则,您将拥有 2 个first commit
  • @Cupcake:你测试过旧版本的命令吗?它应该可以正常工作。修改只是更改提交消息,因此旧的和新的根提交引入完全相同的更改,因此旧的根提交被自动跳过。第二个HEAD 确保考虑所有提交,并且我们可以使用 rebase 的双参数版本移回 master。请注意,这个答案早于--root 选项的存在。
  • ecdpalma's answer 下面更简单,也有更多投票,向下滚动人!
【解决方案3】:

要扩展ecdpalma's answer,您现在可以使用--root 选项告诉rebase 您要重写根/首次提交:

git rebase --interactive --root

然后根提交将显示在 rebase TODO 列表中,您可以选择对其进行编辑或改写:

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

这是the Git rebase docs--root的解释(强调我的):

对所有可从&lt;branch&gt; 访问的提交重新设置基准,而不是用&lt;upstream&gt; 限制它们。 这允许您在分支上重新设置根提交

【讨论】:

    【解决方案4】:

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

    如果您正在创建一个 repo,并且预先知道您将在未来的“第一次”实际提交之上进行 rebase,那么您可以通过在开始时进行显式的空提交来完全避免这个问题:

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

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

    【讨论】:

    • 这是否意味着,为了让它发挥作用,您需要有远见(或通灵)在项目开始时进行空提交 /i>?对我来说,这似乎是极端情境,而且通常不实用。你怎么看?如果我已经进行了 100 次提交,我突然需要编辑根提交会怎样。在那种情况下,如果我没有在开始时进行空提交,这仍然有效吗?
    • 编辑根提交的消息可能不是你在拥有 100 条之后会做的事情。有时我碰巧只是想拥有一个 git repo,做一些无用的提交,知道一旦我达到某种可用状态,我就会将它们压缩成一个状态,然后改写消息。无论如何,现在我改变了主意,我认为第一次提交绝对最有用的事情是放置 .gitattributes 文件而不是进行空提交。
    【解决方案5】:

    你可以使用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
    

    【讨论】:

    • 我正在使用 filter-branch 更改作者/提交者,-- --all 选项确实是在这种情况下能够处理根提交的关键。
    猜你喜欢
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 2012-04-26
    相关资源
    最近更新 更多