G0574's Archiver

弯月圆刀 发表于 2008-10-6 09:57

git 的diff

Unlike the traditional unified diff format, which shows two files A and B with a single column that has - (minus — appears in A but removed in B), + (plus — missing in A but added to B), or " " (space — unchanged) prefix, this format compares two or more files file1, file2,… with one file X, and shows how X differs from each of fileN. One column for each of fileN is prepended to the output line to note how X's line is different from it.

git的diff,特别是merge的时候,比较的对象是working tree version file(X),首先是当前的HEAD版本和X比较,然后是MERGE_HEAD和X比较。

要放弃merge 输入 git reset --hard HEAD

弯月圆刀 发表于 2008-10-6 10:40

这是一个示例diff。[code]HEAD:
a1
a2
yy
a4


MERGE_HEAD:
a1
a2
xx
a3


a1
a2
<<<<<<< HEAD:a.txt
yy
a4
=======
xx
a3
>>>>>>> b1:a.txt



diff --cc a.txt
index 8bbc071,e109bc7..0000000
--- a/a.txt
+++ b/a.txt
@@@ -1,4 -1,4 +1,9 @@@
--a1
--a2
- yy
- a4
-xx
-a3
++a1
++a2
++<<<<<<< HEAD:a.txt
++yy
++a4
++=======
++xx
++a3
++>>>>>>> b1:a.txt[/code]如果按照diff的结果走一遍,就可以从一个文件变化到另一个文件。
比如对于HEAD文件:
a1
a2
yy
a4
按顺序执行:
-a1
-a2
-yy
-a4
+a1
+a2
+<<<<<<< HEAD:a.txt
+yy
+a4
+=======
+xx
+a3
+>>>>>>> b1:a.txt
就变成了结果文件。

同样对于MERGE_HEAD:
a1
a2
xx
a3
走一遍是:
-a1
-a2
-xx
-a3
+a1
+a2
+<<<<<<< HEAD:a.txt
+yy
+a4
+=======
+xx
+a3
+>>>>>>> b1:a.txt

弯月圆刀 发表于 2008-10-6 10:42

所以diff某种程度上可以考虑成变化回放一样。按照diff文件的指示,一步一步的走,就能够从源文件走到目标文件。

弯月圆刀 发表于 2008-10-6 15:24

当你运行 git diff /somepaht/somefile的时候,比较是working tree 和 index中的版本。

一般情况下,就是指你在commit之前修改了什么。如果你刚刚进行了一次commit,那么diff的结果就是空。

[[i] 本帖最后由 弯月圆刀 于 2008-10-6 15:27 编辑 [/i]]

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.