【问题标题】:cherry picking a commit that deletes files樱桃选择删除文件的提交
【发布时间】:2013-07-30 18:05:22
【问题描述】:

我需要将几个存储库(每个存储库都从 TFS 转换)合并为一个。为此,我使用 git cherry-pick 命令,该命令适用于某些提交,但不适用于其他提交:

$ git status
# On branch master
nothing to commit, working directory clean
$ git diff-tree --no-commit-id --name-only -r e2d8405
Libraries/IFileTransformer/ITransformer.cs
Libraries/IFileTransformer/IFileTransformer.csproj
Libraries/IFileTransformer/IFileTransformer.csproj.vspscc
Libraries/IFileTransformer/Properties/AssemblyInfo.cs
$ git cherry-pick e2d8405
error: could not apply e2d8405... TFS changeset 2836
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
$ git status
# On branch master
# You are currently cherry-picking.
#   (fix conflicts and run "git commit")
#
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#       deleted by them:    Libraries/IFileTransformer/ITransformer.cs
#       deleted by them:    Libraries/IFileTransformer/IFileTransformer.csproj
#       deleted by them:    Libraries/IFileTransformer/IFileTransformer.csproj.vspscc
#       deleted by them:    Libraries/IFileTransformer/Properties/AssemblyInfo.cs
#
no changes added to commit (use "git add" and/or "git commit -a")
$

如何找出问题所在? “他们”是谁?在我看来,e2d8405 提交删除了四个文件。如果文件存在(并且确实存在),那么应用提交的问题在哪里?

$ git checkout e2d8405^
Note: checking out 'e2d8405^'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 48b5b2f... TFS changeset 2835   renamed namespace installutils to
 utils
$ md5sum IFileTransformer.csproj
9f9851dc9db3bddd1e6920631fa14e8b *IFileTransformer.csproj
$ git checkout master
Previous HEAD position was 48b5b2f... TFS changeset 2835   renamed namespace ins
tallutils to utils
Switched to branch 'master'
$ md5sum IFileTransformer.csproj
9f9851dc9db3bddd1e6920631fa14e8b *IFileTransformer.csproj

【问题讨论】:

  • "Them" 是您挑选的提交。 “我们”是您选择的分支。
  • 谢谢,这就是我的怀疑:提交删除了四个文件。但是那个操作的问题在哪里......?

标签: git


【解决方案1】:

虽然该提交会删除这些文件,但您有一些在已删除版本中不存在的修改。由于这里存在冲突,在修改文件的分支和删除它们的分支之间,您需要通过表明您很乐意放弃更改并应用删除 (git rm ...) 来解决它。完成后,git commit 创建精选提交。

【讨论】:

  • 我怎样才能知道这些变化是什么?在我看来,主文件和之前提交的文件是相同的(添加的命令和结果显示它的问题)。
  • 检查它们有可能会丢弃空白更改。尝试git diff HEAD e2d8405^ Libraries/IFileTransformer/ITransformer.cs Libraries/IFileTransformer/IFileTransformer.csproj Libraries/IFileTransformer/IFileTransformer.csproj.vspscc Libraries/IFileTransformer/Properties/AssemblyInfo.csgit log -p HEAD...e2d8405^ &lt;same files&gt; 来查看提交。
  • 确实,这些文件中的行尾不同。
  • 好的,现在我知道出了什么问题,但我该如何解决呢?如何让樱桃采摘忽略空白差异?
【解决方案2】:

运行git mergetool,它会告诉您为什么它看到已删除文件的冲突,该文件可能在本地更改并在精心挑选的“他们”提交中删除。它要您决定是保留修改后的版本还是删除文件。

然后您可以选择要对这些文件执行的操作。

【讨论】:

    【解决方案3】:

    git 合并的工作方式是这样的:

    对于每个要合并的文件,找到包含 并且存在于要合并的两个分支中的最新提交。然后搜索要合并的每个分支并查找任何后续更改,这些更改将仅出现在其中一个分支中。

    • 如果两个分支中没有此类更改,请保持文件不变
    • 如果其中一个分支对文件进行了更改,而另一个没有,则保留有更改的分支
    • 如果两个分支都有变化,那么 git 就不能自动合并(哪些变化应该保留,哪些应该被丢弃 - 没有人工干预不可能说)

    你所拥有的是最后一种情况,其中文件在一个分支中被删除,但在最后一次合并两个分支之后在另一个分支中被更改。

      A 1* B
       / \
      2   3
      |   |
      |   4*
      5*  |
      |   |
    

    这里的数字是commits,*表示commit change一个我们感兴趣的文件包含在commit中。所以,在提交 1 处,我们还没有分支出来,这意味着这个提交是基本提交,是两个分支中都存在的最新提交。在分支之后,向每个分支添加一个提交,但都不包括有问题的文件。在此时合并时,文件只是在提交 1 中保持不变。然后在提交 4 时,对分支 B 中的文件进行更改。如果此时我们要将分支 B 合并到分支 A,所做的更改提交 4 中的文件将被合并到分支 A,因为它是最新的提交,并且在基本提交 (1) 之后分支 A 中的文件没有任何更改。但是在提交 5 时,该文件在分支 A 中也发生了更改。现在,如果我们尝试合并(无论哪种方式都会产生相同的结果),我们将遇到冲突,因为文件在两个分支中都已更改(在基本提交之后),并且 git 无法选择其中一个为“正确” ',因此由您决定。

    检查分支中不包含删除的文件的文件历史记录,然后您可能会发现此分支中的提交,而不是删除它们的分支。

    【讨论】:

    • 自动合并是对两个不同的文件及其共同祖先应用三路合并算法,用于逐行合并更改。当两个分支都发生变化时使用它。仅当 automerge 无法产生输出时,才需要手动干预。如果只更改一侧,则完全没有必要使用 Automerge。
    猜你喜欢
    • 2018-08-14
    • 2014-11-21
    • 2016-05-30
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多