【问题标题】:Git rebase interactive the last n commitsGit rebase 交互式最后 n 次提交
【发布时间】:2017-01-04 13:16:33
【问题描述】:

我在我的功能分支中做了一堆未推送的提交,现在想要重新排序并在视觉上部分压缩归属提交。我认为解决方案在某种程度上在于 Git 交互,但是如何调用它呢?

$ git rebase --interactive --onto <the-ID-of-the-first-commit-to-rewrite>

只是弹出带有a的VI

noop

内容后跟注释信息。退出后,我的 head 被重置为指定的提交。

如何正确触发交互式变基以修改自某个提交以来的提交?

【问题讨论】:

  • 你的触发是正确的,你只需要学习如何使用它。阅读注释掉的部分,它包含关于你应该做什么的简要说明。

标签: git git-rebase


【解决方案1】:

你应该使用

git rebase --interactive <sha1>

&lt;sha1&gt; 应该是您要重写的第一个提交的 sha,而是之前提交的 sha。

如果你的历史是这样的:

pick 43576ef last commit
...
pick 5116d42 first commit to rewrite
pick cb85072 last good commit

那么你可以有不同的方式来指示要基于哪个提交:

git rebase -i cb85072
git rebase -i 5116d42^

在哪里

  • ^ 表示之前的提交。
  • -i--interactive 的缩写

【讨论】:

  • 看起来是“onto”引起了问题。
【解决方案2】:

您也可以从上次提交后退一些提交。例如,如果你想 rebase 最近 5 次提交,你可以使用这个命令: git rebase -i HEAD~5.

【讨论】:

    【解决方案3】:

    要查看和重写最后的 n 提交,请使用:

    git rebase -i HEAD~n
    

    p,pick = 使用提交

    f, fixup = 类似“squash”,但丢弃此提交的日志消息

    https://www.freecodecamp.org/forum/t/how-to-squash-multiple-commits-into-one-with-git-squash/13231

    【讨论】:

      【解决方案4】:

      公认的答案是正确的

      不过,计算 n 次提交到 squash 并为 rebase 选择提交 id 是很棘手的

      git rebase -i HEAD~[N]   // N is the number of commits, starting from the most recent one
      

      git rebase -i HEAD~[7]

      但如果你有大量的壁球承诺

      git rebase -i [commit-id] // [commit-id] is the hash of the commit just before the first one 
      

      git rebase -i 6394dc


      Inspired by

      【讨论】:

        【解决方案5】:

        如果您想使用其最后 N 次提交以交互方式将分支 B 重新定位到分支 A,您通常可以这样做:

        git rebase -i --onto A B~N B

        例如

        git rebase -i --onto master feature~3 feature

        在非交互的情况下也能很好地工作 - 没有-i

        【讨论】:

          【解决方案6】:

          我想念你指令中的动作rebase

          git rebase -i <id-of-commit>
          

          【讨论】:

          • 是的,是一个错字。我已经解决了这个问题。
          猜你喜欢
          • 2011-09-23
          • 1970-01-01
          • 2017-10-27
          • 2010-12-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多