git重命名/删除混淆 [英] git rename/delete confusion

查看:104
本文介绍了git重命名/删除混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是步骤和情况(命令列表也在后面给出):


  1. 我有两个分支:master和XBranch
  2. 它们都有一个src / a.txt文件。它的内容是旧内容

  3. 在XBranch中,我使用以下命令将src / a.txt重命名为src / b.txt: mv git rm git add

  4. 文件a.txt。在提交过程中,我做了 git rm src / a.txt 但忘记做 git add src / b.txt
    在master中我做的是: git rm src / a.txt git commit


  5. 在master中,我将文件b.txt的内容编辑为新内容


  6. 在master中,我做了 git add src / b.txt git commit

  7. 在master中我做: git merge XBranch

文件src / b.txt冲突,这是完全可以理解的。
但是内容是旧内容。为什么?



为什么不是是这样的:

 << <<<<<<<<<<<<<< b>>>>>>> > XBranch 



命令清单:



<$ SABYA-PC d:/ merge_temp / test / case2
$ mkdir源

sabya @ SABYA-PC d:/ merge_temp / test / case2
$ git init
在d:/merge_temp/test/case2/.git/

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ mkdir src $ b中初始化了空的Git仓库
$ b sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ vi src / a.txt

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ cat src / a.txt
旧内容

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ git add src /

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ git commit
[master(root-commit)148500e ]添加src / a.txt
1个文件改变,1个插入(+),0个删除( - )
创建模式100644 src / a.txt

sabya @ SABYA- PC d:/ merge_temp / test / case2(master)
$ git branch XBranch

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ git结帐XBranch
转换到分支'XBranch'

sabya @ SABYA-PC d:/ merge_temp / test / case2(XBranch)
$ mv src / a.txt src / b .txt

sabya @ SABYA-PC d:/ merge_temp / test / case2(XBranch)
$ git rm src / a.txt
rm'src / a.txt'

sabya @ SABYA-PC d:/ merge_temp / test / case2(XBranch)
$ git add src / b.txt

sabya @ SABYA-PC d:/ merge_temp / test / case2(XBranch)
$ git commit
[XBranch b3ff8fa]将a.txt更改为b.txt在XBranch中
1个文件已更改,0个插入(+),0个删除( - )
重命名src / {a.txt => b.txt}(100%)

sabya @ SABYA-PC d:/ merge_temp / test / case2(XBranch)
$ git checkout master
转换到分支'master'

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ mv src / a.txt src / b.txt

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ git rm src / a.txt
rm'src / a.txt'

sabya @ SABYA- PC d:/ merge_temp / test / case2(master)
$ git commit
[master bfeaecb]已删除src / a.txt
1个文件已更改,0个插入(+),1个删除( - )
删除模式100644 src / a.txt

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ vi src / b.txt

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ cat src / b.txt
新内容

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ git add src / b.txt

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ git commit
[master 2361d5e]更改b.txt的内容
更改1个文件,1个插入(+),0个删除( - )
创建模式100644 src / b.txt

sabya @ SABYA-PC d:/ merge_temp / test / case2(master)
$ git merge XBranch
CONFLICT(重命名/删除):在XBranch中重命名src / a.txt-> src / b.txt并在HEAD
中删除自动合并失败;修复冲突,然后提交结果。

sabya @ SABYA-PC d:/ merge_temp / test / case2(master | MERGING)
$ cat src / b.txt
旧内容
内容存在冲突,但不涉及文件 内容。它是关于
的内容。 case2 目录(在master中),有一个文件 b.txt
  • 在同一个目录中(在XBranch中),有一个重命名的文件 a.txt => b.txt



  • 当您解决冲突时,您实际上选择一个文件或
    其他(文件内没有一行)。因此,生成的
    文件中的旧内容。






    OP在评论中增加: p>


    但是,在逻辑上它与以下情况有何不同:


    1. 我用<旧内容>在 master 中添加一个文件 a.txt 并提交。

    2. 我用 XBranch 中添加一个文件 a.txt 和提交它。

    3. 我将 XBranch 合并为 master
      这一次它显示了该文件中的两个内容!


    这次,树(分支中的case2目录 master XBranch
    引用a new 文件 a.txt :其内容合并,冲突
    分辨率。之前,在 a.txt
    已重命名为)之间发生了冲突,如 b.txt )和一个新的 b.txt :两个文件不能存在于
    同一分支中,选择(文件而不是文件内容)

    lockquote>

    在我的问题的第4步中,如果我做了 git rm git add ,它可以像我预期的那样工作!我现在不明白这一点。
    如何预测文件何时会同时包含这两个内容?
    当它只包含 XBranch 的内容时,它将只有
    的内容 master

    这意味着:


    • 而不是合并 XBranch a.txt 改名为 b.txt )转换为 master 在步骤6中用新的 b.txt 提交
      (冲突
    • 你会合并 XBranch a.txt 已重命名作为 b.txt )与新的
      中的master(步骤4)( a.txt 也被重命名为为 b.txt ):相同的树内容,但不同的
      blob内容
      :行冲突。
    • >





    就是说,OP仍然认为必须有一个bug:




    • git mailing-列表问题

    • GitHub支持问题

    • ul>

      I having one confusion about a particular git behavior:

      Following are the steps and situation (the list of commands are also given later):

      1. I have two branches: master and XBranch
      2. There is a file src/a.txt in both of them. It's content is "Old Content"
      3. In XBranch I rename src/a.txt to src/b.txt, using: mv, git rm, git add.
      4. In master rename the file a.txt. During commit I did git rm src/a.txt but forgot to do git add src/b.txt In master I do: git rm src/a.txt and git commit

      5. In master, I edit the content of the file b.txt to "New Content

      6. In master I do git add src/b.txt and git commit
      7. In master I do: git merge XBranch

      The file src/b.txt conflicts, which is perfectly understandable. But the content is "Old Content". Why?

      Why not is it something like:

      <<<<<<< HEAD
      New Content
      =======
      Old content
      >>>>>>> XBranch
      

      List of Commands:

      sabya@SABYA-PC d:/merge_temp/test/case2
      $ mkdir source
      
      sabya@SABYA-PC d:/merge_temp/test/case2
      $ git init
      Initialized empty Git repository in d:/merge_temp/test/case2/.git/
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ mkdir src
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ vi src/a.txt
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ cat src/a.txt
      Old Content
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ git add src/
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ git commit
      [master (root-commit) 148500e] added src/a.txt
       1 files changed, 1 insertions(+), 0 deletions(-)
       create mode 100644 src/a.txt
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ git branch XBranch
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ git checkout XBranch
      Switched to branch 'XBranch'
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (XBranch)
      $ mv src/a.txt src/b.txt
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (XBranch)
      $ git rm src/a.txt
      rm 'src/a.txt'
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (XBranch)
      $ git add src/b.txt
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (XBranch)
      $ git commit
      [XBranch b3ff8fa] changed a.txt to b.txt in XBranch
       1 files changed, 0 insertions(+), 0 deletions(-)
       rename src/{a.txt => b.txt} (100%)
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (XBranch)
      $ git checkout master
      Switched to branch 'master'
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ mv src/a.txt src/b.txt
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ git rm src/a.txt
      rm 'src/a.txt'
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ git commit
      [master bfeaecb] removed src/a.txt
       1 files changed, 0 insertions(+), 1 deletions(-)
       delete mode 100644 src/a.txt
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ vi src/b.txt
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ cat src/b.txt
      New Content
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ git add src/b.txt
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ git commit
      [master 2361d5e] changed content of b.txt
       1 files changed, 1 insertions(+), 0 deletions(-)
       create mode 100644 src/b.txt
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master)
      $ git merge XBranch
      CONFLICT (rename/delete): Rename src/a.txt->src/b.txt in XBranch and deleted in HEAD
      Automatic merge failed; fix conflicts and then commit the result.
      
      sabya@SABYA-PC d:/merge_temp/test/case2 (master|MERGING)
      $ cat src/b.txt
      Old Content
      

      解决方案

      There is a conflict, but not about the file content. It is about the tree content.

      • In one tree, the case2 directory (in master), there is a new file b.txt
      • in the same directory (in XBranch), there is a renamed file a.txt => b.txt

      When you are resolving the conflict, you are in effect choosing one file or the other (not one line within the file). Hence the "Old Content" in the resulting file.


      The OP adds in the comments:

      But then how logically is it different from the following situation:

      1. I add a file a.txt in master with "Old Content" and commit it.
      2. I add a file a.txt in XBranch with "New Content" and commit it.
      3. I merge XBranch into master. This time it is showing both contents in that file!

      This time, both trees (the case2 directory in branches master and XBranch) reference a new file a.txt: its content get merged, with conflict resolution. Before, there was a conflict between a a.txt (renamed as b.txt) and a new b.txt: both files cannot exist in the same branch, a choice (of file, not of file content) had to be made.

      In step 4 of my question, if I do "git rm" and "git add" in a single commit, it works as I expect! I fail to understand that now. How can I predict when the file will have both contents? When it will just have the content of XBranch and when will it have just the content of master?

      That means that:

      • instead of merging XBranch (a.txt renamed as b.txt) to master commit with a new b.txt from step 6 (conflict of tree),
      • you would merge XBranch (a.txt renamed as b.txt) with master from new step 4 (a.txt also renamed as b.txt): same tree content, but different blob content: conflict of lines.

      That being said, the OP still thinks there must be a bug:

      这篇关于git重命名/删除混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆