git重命名/删除混淆 [英] git rename/delete confusion
问题描述
以下是步骤和情况(命令列表也在后面给出):
- 我有两个分支:master和XBranch
- 它们都有一个src / a.txt文件。它的内容是旧内容
- 在XBranch中,我使用以下命令将src / a.txt重命名为src / b.txt:
mv
,git rm
,git add
。 -
文件a.txt。在提交过程中,我做了
git rm src / a.txt
但忘记做git add src / b.txt
在master中我做的是:git rm src / a.txt
和git commit
-
在master中,我将文件b.txt的内容编辑为
新内容
- 在master中,我做了
git add src / b.txt
和git commit
- 在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
a.txt => b.txt
当您解决冲突时,您实际上选择一个文件或
其他(文件内没有一行)。因此,生成的
文件中的旧内容。
OP在评论中增加: p>
但是,在逻辑上它与以下情况有何不同:
- 我用<旧内容>在
master
中添加一个文件a.txt
并提交。
- 我用
在 XBranch
中添加一个文件a.txt
和提交它。
- 我将
XBranch
合并为master
。
这一次它显示了该文件中的两个内容!
这次,树(分支中的case2目录 在我的问题的第4步中,如果我做了 这意味着: 就是说,OP仍然认为必须有一个bug: I having one confusion about a particular git behavior: Following are the steps and situation (the list of commands are also given later): In master rename the file a.txt. During commit I did In master, I edit the content of the file b.txt to " The file src/b.txt conflicts, which is perfectly understandable.
But the content is " Why not is it something like:
There is a conflict, but not about the file content. It is about the
tree content. 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:
This time, both trees (the case2 directory in branches In step 4 of my question, if I do " That means that: That being said, the OP still thinks there must be a bug: 这篇关于git重命名/删除混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! master
和 XBranch
)
引用a new 文件 a.txt
:其内容合并,冲突
分辨率。之前,在 a.txt
(已重命名为)之间发生了冲突,如 b.txt
)和一个新的 b.txt :两个文件不能存在于
同一分支中,选择(文件而不是文件内容)
lockquote>
git rm
和 git add
,它可以像我预期的那样工作!我现在不明白这一点。
如何预测文件何时会同时包含这两个内容?
当它只包含 XBranch
的内容时,它将只有
的内容 master $ c
XBranch
( a.txt
改名为 b.txt
)转换为
master
在步骤6中用新的 b.txt
提交
(冲突
XBranch
( a.txt
已重命名作为 b.txt
)与新的
中的master(步骤4)( a.txt
也被重命名为为 b.txt
):相同的树内容,但不同的
blob内容:行冲突。
ul>
mv
, git rm
, git add
.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
New Content
git add src/b.txt
and git commit
git merge XBranch
Old Content
". Why?<<<<<<< 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
case2
directory (in master), there is a new file b.txt
a.txt => b.txt
a.txt
in master
with "Old Content" and commit it.a.txt
in XBranch
with "New Content" and commit it.XBranch
into master
.
This time it is showing both contents in that file!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.
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
?
XBranch
(a.txt
renamed as b.txt
) to master
commit
with a new b.txt
from step 6 (conflict of tree),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.