【问题标题】:How to use interactive rebase on the first (root) commit of a branch?如何在分支的第一个(根)提交上使用交互式变基?
【发布时间】:2015-07-28 10:04:28
【问题描述】:

如果我处于以下情况,

$ git log --oneline
* abcdef commit #b
* 123456 commit #a

我知道我总是可以跑步

$ git reset HEAD~ 
$ git commit --amend

但是,我尝试运行

$ git rebase -i HEAD~2

但我得到了

fatal: Needed a single revision
invalid upstream HEAD~2

因此我的问题是:有没有办法使用git rebase 来压缩这两个提交?

【问题讨论】:

    标签: git rebase squash


    【解决方案1】:

    我来这个问题是为了寻找标题的答案。在一个非常大的 repo 上,接受的答案会为主分支(又名:master)中的每个提交产生一个交互式 rebase,而不是 just 用于给定分支。对于来到这里的其他人,替代方法是使用父分支名称(或提交):

    git rebase -i <base_branch_name>

    在挖掘 git 文档并找到这个 (https://git-scm.com/docs/git-rebase#_interactive_mode) 后,我意识到了这一点:

    从你想按原样保留的最后一个提交开始:

    git rebase -i <after-this-commit>

    编辑器会被所有的提交触发 您当前的分支(忽略合并提交),它位于 给定提交。

    假设您从 master 分支了 branch_a,并且您希望以交互方式将所有提交重新设置为 branch_a。要做到这一点,你会这样做:

    git rebase -i master

    或者,如果您想从中间(或任何提交)开始,您可以这样做:

    git rebase -i <some-commit-hash-in-the-middle>

    另一种常见的形式是,如果您知道“我想从现在的位置退回 5 个提交”

    git rebase -i HEAD~5

    希望这有助于其他人在他们的 git rebase 打开一个包含数千行提交的编辑器时避免心脏病发作。 ─=≡Σ((( つ><)つ

    【讨论】:

      【解决方案2】:

      你想变基到你的master 分支的根提交。更具体地说,要压缩两个提交,您需要运行

      git rebase -i --root
      

      然后在弹出的编辑器缓冲区的第二行用squash替换pick

      pick 123456 a                                                        
      squash abcdef b
      

      我将您推荐给git-rebase man page,以了解有关该标志的更多详细信息:

      --root

      Rebase 所有可从<branch> 访问的提交,而不是 用<upstream> 限制它们。这允许您重新设置基准 分支上的根提交。 [...]

      根的交互式变基示例

      # Set things up
      $ mkdir testgit
      $ cd testgit
      $ git init
      
      # Make two commits
      $ touch README
      $ git add README
      $ git commit -m "add README"
      $ printf "foo\n" > README
      $ git commit -am "write 'foo' in README"
      
      # Inspect the log
      $ git log --oneline --decorate --graph
      * 815b6ca (HEAD -> master) write 'foo' in README
      * 630ede6 add README
      
      # Rebase (interactively) the root of the current branch: 
      # - Substitute 'squash' for 'pick' on the second line; save and quit the editor.
      # - Then write the commit message of the resulting commit; save and quit the editor.
      $ git rebase -i --root
      [detached HEAD c9003cd] add README; write 'foo' in README
       Date: Sat May 16 17:38:43 2015 +0100
       1 file changed, 1 insertion(+)
       create mode 100644 README
      Successfully rebased and updated refs/heads/master.
      
      # Inspect the log again
      $ git log --oneline --decorate --graph
      * c9003cd (HEAD -> master) add README; write 'foo' in README
      

      【讨论】:

        【解决方案3】:

        看来这个参数可能会有所帮助:

        --root 
        

        重新设置从 可到达的所有提交,而不是用 限制它们。这允许您重新设置根 在分支上提交。

        这应该可以让您将第二次提交压缩(我猜您实际上想要修复)第一次提交:

        git rebase --root -i
        

        请注意理解 --root 选项的作用,因为在您的情况下,它可以满足您的需求,但例如在分支中使用时可能会很棘手,因为它将重新定位到历史上可到达的最远祖先(即树的根);所以rebase --root 将在aA-B-D-E-X-Y-Z 上重新定位z

        master      A-B-C
                       \
        upstream        D-E  
                           \     
        current branch      X-Y-Z
        

        【讨论】:

          猜你喜欢
          • 2014-05-21
          • 2021-06-18
          • 1970-01-01
          • 2015-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-12
          相关资源
          最近更新 更多