【问题标题】:Is there an equivalent of "git apply --reject" for git rebase?git rebase 是否有相当于“git apply --reject”的东西?
【发布时间】:2014-05-27 15:49:51
【问题描述】:

我想使用git rebase 进行一系列提交并将它们应用于不同的根提交。例如,

git rebase --onto root start finish

根据root 获取从startfinish 的提交。

当 git 无法干净地应用提交时,它会更新文件以显示这样的冲突(来自 git 手册的示例):

       Here are lines that are either unchanged from the common
       ancestor, or cleanly resolved because only one side changed.
       <<<<<<< yours:sample.txt
       Conflict resolution is hard;
       let's go shopping.
       =======
       Git makes conflict resolution easy.
       >>>>>>> theirs:sample.txt
       And here is another line that is cleanly resolved or unmodified.

程序员将文件编辑为新分支中应有的内容,然后运行git --rebase continue 以继续从源添加提交。

但是,当rootstart 之间的文件有很多更改时,可能会有很多这样的行并且它们可能难以解释。在这种情况下,人们可能更喜欢将“失败的大块”输出到文件中,以便可以阅读原始提交中的更改(手动对正在更改的文件进行必要的更改,然后运行 ​​git rebase --continue 到继续添加提交)。

git apply--reject 选项执行此操作:

   --reject
       For atomicity, git apply by default fails the whole patch and does
       not touch the working tree when some of the hunks do not apply.
       This option makes it apply the parts of the patch that are
       applicable, and leave the rejected hunks in corresponding *.rej
       files.

这也是patch 程序的行为——所以我可以通过首先使用git show 输出提交,然后使用patch 应用它来获得我想要的。但是,当涉及到许多提交时,这并不方便。

有没有办法使用git rebase(或其他 git 命令)来做到这一点?

【问题讨论】:

  • 您是否考虑过使用视觉差异/合并工具来帮助您解释冲突标记?我使用 Beyond Compare,我发现它为我节省了很多时间来修复合并冲突。
  • 我不知道它们是什么。它们是否允许您指定大块适用于文件中的哪个位置? (我已经重新组织了文件并且 git 或 patch 不知道将这些大块应用到哪里,这就是为什么我想手动完成所有操作。)
  • 您使用的是什么操作系统,Windows、OS X 还是 Linux?
  • Beyond Compare 有一个 Linux 版本,您可以尝试一下。 Linux 可能还有其他差异/合并工具。 vimdiff 是您的 Linux 发行版可能附带的另一个工具,但在我看来,它不如 Beyond Compare 之类的工具好。阅读功能或查看屏幕截图,看看它是否能帮助您更轻松地解决合并冲突。
  • 我对批量拒绝这样的拒绝持谨慎态度,因为您对早期拒绝的解决可能很容易影响以后出现的冲突。一旦你的工具为你工作,交互式 rebase 就会变得非常快。 Git 只是盒子里的一个工具,你的编辑器是另一个。

标签: git patch rebase


【解决方案1】:

您可以在逐个提交的基础上执行类似的操作。

当发生合并冲突时,git 会给出如下消息:

CONFLICT (content): Merge conflict in file.c
Failed to merge in the changes.
Patch failed at 0004 Changes to file
The copy of the patch that failed is found in:
   /home/g/src/project/.git/rebase-apply/patch

如果 file.c 由于显示文件中的合并冲突而被破坏,您可以将文件恢复到提交之前的状态

git reset file.c
git checkout file.c

然后你可以运行

patch -p1 </home/g/src/project/.git/rebase-apply/patch

这个输出可能看起来像

patching file file.c
Hunk #2 FAILED at 1133.
Hunk #3 FAILED at 1167.
Hunk #4 FAILED at 1201.
Hunk #5 FAILED at 1241.
Hunk #6 FAILED at 1251.
Hunk #7 succeeded at 1324 (offset 6 lines).
Hunk #8 FAILED at 1325.
Hunk #9 succeeded at 2142 (offset 11 lines).
Hunk #10 succeeded at 2163 (offset 11 lines).
Hunk #11 succeeded at 2181 (offset 11 lines).
Hunk #12 succeeded at 2279 (offset 11 lines).
Hunk #13 succeeded at 2299 (offset 11 lines).
Hunk #14 succeeded at 2412 (offset 11 lines).
Hunk #15 succeeded at 2508 (offset 11 lines).
Hunk #16 succeeded at 2531 (offset 11 lines).
Hunk #17 succeeded at 2540 (offset 11 lines).
Hunk #18 succeeded at 2581 (offset 11 lines).
Hunk #19 succeeded at 2599 (offset 11 lines).
Hunk #20 succeeded at 2611 (offset 11 lines).
Hunk #21 succeeded at 2629 (offset 11 lines).
Hunk #22 succeeded at 2637 (offset 11 lines).
Hunk #23 succeeded at 2668 (offset 11 lines).
Hunk #24 succeeded at 2677 (offset 11 lines).
Hunk #25 succeeded at 2805 (offset 11 lines).
Hunk #26 succeeded at 2871 (offset 11 lines).
Hunk #27 succeeded at 2911 (offset 11 lines).
Hunk #28 succeeded at 3028 (offset 11 lines).
Hunk #29 succeeded at 3085 (offset 11 lines).
Hunk #30 succeeded at 3117 (offset 11 lines).
Hunk #31 succeeded at 3557 (offset 11 lines).
Hunk #32 succeeded at 3572 (offset 11 lines).
Hunk #33 succeeded at 4773 (offset 11 lines).
Hunk #34 succeeded at 4807 (offset 11 lines).
Hunk #35 succeeded at 4859 (offset 11 lines).
6 out of 35 hunks FAILED -- saving rejects to file file.c.rej

然后手动修改file.c.rej,然后运行

git add -u
git rebase --continue

继续。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-21
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多