【问题标题】:Workaround git commit解决方法 git commit
【发布时间】:2013-02-02 19:40:52
【问题描述】:

假设我有 3 个 git 提交:

  1. 在侧边栏提交更改
  2. 提交页脚更改
  3. 提交对标头的更改

现在假设我已经经历了精神觉醒并意识到我需要的唯一更改是在页眉和侧边栏上,页脚很好,不需要更改。

是否有可以执行以下操作的命令?

  • 合并提交#3 减去提交#2
  • 删除提交#2
  • 合并提交#3 和提交#1

(或任何其他使 commit#2 从未发生过的方法)。

【问题讨论】:

    标签: git merge commit ignore


    【解决方案1】:

    交互式 rebase 允许您修改、重新排列、合并提交、更改消息等:

    git rebase --interactive
    

    引用帮助:

    # Commands:
    #  p, pick = use commit
    #  r, reword = use commit, but edit the commit message
    #  e, edit = use commit, but stop for amending
    #  s, squash = use commit, but meld into previous commit
    #  f, fixup = like "squash", but discard this commit's log message
    #  x, exec = run command (the rest of the line) using shell
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    # However, if you remove everything, the rebase will be aborted.
    #
    

    丢失提交是你想要的。

    【讨论】:

      【解决方案2】:

      您始终可以使用 git-revert 恢复单个提交的更改。

      给定一个或多个现有提交,还原相关补丁引入的更改,并记录一些记录它们的新提交。这要求您的工作树是干净的(不修改 HEAD 提交)。

      注意:git revert 用于记录一些新的提交,以扭转一些早期提交的影响(通常只是一个错误的提交)。如果您想丢弃工作目录中所有未提交的更改,您应该看到git-reset(1),尤其是--hard 选项。如果你想提取在另一个提交中的特定文件,你应该看到git-checkout(1),特别是git checkout <commit> -- <filename> syntax。请注意这些替代方案,因为它们都会丢弃您工作目录中未提交的更改。

      所以:

      git revert <commit-id-of-footer-changes>
      

      【讨论】:

        【解决方案3】:

        您可以使用cherry-pick 合并您想要的提交,然后使用revert 开发分支在页脚更改之前提交。

        git checkout branch_to_merge_into
        git cherry-pick commit#1 commit#3
        

        【讨论】:

          猜你喜欢
          • 2017-05-15
          • 1970-01-01
          • 1970-01-01
          • 2014-01-12
          • 2021-11-05
          • 2018-12-07
          • 2014-01-17
          • 1970-01-01
          相关资源
          最近更新 更多