Ben*_*min 3 git merge conflict
我只是测试git,看看我是否可以将它用于我的工作.我遇到了一个似乎很小的问题,但可能会成为真正的代码.我的文件看起来像:text.txt 1 2 3 4我有一个本地分支"branch1"并在分支和主服务器中提交更改.在主人我改变了第二行的第一行.所以diff for master看起来像这样:
+1 master
2
3
4
Run Code Online (Sandbox Code Playgroud)
对于分支它是:
1
-2
+2b1
3
4
Run Code Online (Sandbox Code Playgroud)
运行git merge branch1解决冲突:
<<<<<<< HEAD
1 master
2
=======
1
2b1
>>>>>>> branch1
3
4
Run Code Online (Sandbox Code Playgroud)
我知道这个可以轻松解决.但无论如何,这又是一场冲突.不应该git能合并吗?
几点评论:
首先,这样一个小例子永远不会被合并:
warning: Cannot merge binary files: afile.txt (HEAD vs. abranch)
那么,如果你有许多"小"合并冲突,你知道它们应该独立于上下文来解决,你可以尝试首先在你的分支上重新定位master,忽略上下文:
git rebase -C0 master
然后将您的分支合并到master.
忽略rebase的所有上下文通常不是一个好主意,但如果你确定你的修改(如"根本不需要上下文的修改"),它将起作用.
-C<n>
Run Code Online (Sandbox Code Playgroud)
确保在
<n>每次更改之前和之后至少有一些周围环境匹配.
当存在较少的周围环境线时,它们都必须匹配.
默认情况下,不会忽略任何上下文.
你可以很容易地测试它(在PowerShell会话中,在Xp上使用Git1.6.5.1)
首先创建一个小棒实用程序 genfile.bat
echo hello, World %1 > afile.txt
echo hello, World %2 >> afile.txt
echo hello, World 3 >> afile.txt
echo hello, World 4 >> afile.txt
echo hello, World 5 >> afile.txt
Run Code Online (Sandbox Code Playgroud)
然后创建一个repo并添加一个文件:
PS D:\git\tests\mergeLines> git init m0
PS D:\git\tests\mergeLines> cd m0
PS D:\[...]\m0> D:\git\tests\mergeLines\genfile.bat 1 2
PS D:\[...]\m0> git add -A
PS D:\[...]\m0> git ci -m "afile to be modified concurrently"
Run Code Online (Sandbox Code Playgroud)
您的文件如下所示:
hello, World 1
hello, World 2
hello, World 3
hello, World 4
hello, World 5
Run Code Online (Sandbox Code Playgroud)
在分支中修改它
PS D:\[...]\m0> git co -b abranch
PS D:\[...]\m0> D:\git\tests\mergeLines\genfile.bat 1 2_modified
PS D:\[...]\m0> git ci -a -m "afile modified in abranch"
Run Code Online (Sandbox Code Playgroud)
你将会有:
hello, World 1
hello, World 2_modified
hello, World 3
hello, World 4
hello, World 5
Run Code Online (Sandbox Code Playgroud)
然后在master中修改它
PS D:\[...]\m0> git co master
PS D:\[...]\m0> D:\git\tests\mergeLines\genfile.bat 1_master 2
PS D:\[...]\m0> git ci -a -m "afile modified in master"
Run Code Online (Sandbox Code Playgroud)
哪个给你:
hello, World 1_master
hello, World 2
hello, World 3
hello, World 4
hello, World 5
Run Code Online (Sandbox Code Playgroud)
克隆第一个实验的回购(即:合并abranch为master)
PS D:\[...]\m0> cd ..
PS D:\git\tests\mergeLines> git clone m0 m1
PS D:\git\tests\mergeLines> cd m1
PS D:\[...]\m1> git co -b abranch origin/abranch
PS D:\[...]\m1> git co master
PS D:\[...]\m1> git merge abranch
Run Code Online (Sandbox Code Playgroud)
这给你一个冲突:
Auto-merging afile.txt
CONFLICT (content): Merge conflict in afile.txt
Automatic merge failed; fix conflicts and then commit the result.
PS D:\[...]\m1> type afile.txt
<<<<<<< HEAD
hello, World 1_master
hello, World 2
=======
hello, World 1
hello, World 2_modified
>>>>>>> abranch
hello, World 3
hello, World 4
hello, World 5
Run Code Online (Sandbox Code Playgroud)
再次克隆第一个回购,这次首先abranch在master没有上下文的情况下进行rebase :
PS D:\[...]\m1> cd ..
PS D:\git\tests\mergeLines> git clone m0 m2
PS D:\git\tests\mergeLines> cd m2
PS D:\[...]\m2> git co -b abranch origin/abranch
PS D:\[...]\m2> git rebase -C0 master
Run Code Online (Sandbox Code Playgroud)
您的文件以静默方式合并:
hello, World 1_master
hello, World 2_modified
hello, World 3
hello, World 4
hello, World 5
Run Code Online (Sandbox Code Playgroud)
当然,如果您切换回master和现在合并abranch,其结果将是一个快进合并.
PS D:\git\tests\mergeLines\m2> git co master
Switched to branch 'master'
PS D:\git\tests\mergeLines\m2> git merge abranch
Updating c8f48b4..8bee1d2
Fast forward
afile.txt | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
Run Code Online (Sandbox Code Playgroud)