这次说一下通过命令行去批量解决文件冲突的问题, 这个方法适用你清楚的知道自己需要保留哪一个分支, 而不是两个分支里的改动都要一点

关键命令

checkout 是个好方法, 能粗暴的解决很多问题

针对冲突文件保留自己的分支

1
git checkout --ours path/to/conflict_file

想保留别人的分支

1
git checkout --theirs path/to/conflict_file

批量的解决:

1
grep -lr '<<<<<<<' . | xargs git checkout --ours

一个鲜活的例子…

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
test mkdir merge-demo
test cd merge-demo
➜  merge-demo ls
➜  merge-demo echo 'first commit' > index.txt
➜  merge-demo cat index.txt
first commit
➜  merge-demo git init
Initialized empty Git repository in /Users/gpf/Documents/test/merge-demo/.git/
➜  merge-demo git:(master) ✗ git add -A
➜  merge-demo git:(master) ✗ git commit -m 'init'
[master (root-commit) e9f17b3] init
 1 file changed, 1 insertion(+)
 create mode 100644 index.txt
➜  merge-demo git:(master) git checkout -b test
Switched to a new branch 'test'
➜  merge-demo git:(test) echo 'second commit by test' > index.txt
➜  merge-demo git:(test) ✗ git add -A && git commit -m 'second commit'
[test 4d20ff3] second commit
 1 file changed, 1 insertion(+), 1 deletion(-)
➜  merge-demo git:(test) cat index.txt
second commit by test
➜  merge-demo git:(test) git checkout master
Switched to branch 'master'
➜  merge-demo git:(master) echo 'third commit by master' > index.txt
➜  merge-demo git:(master) ✗ git add -A && git commit -m 'third commit'
[master 85e520e] third commit
 1 file changed, 1 insertion(+), 1 deletion(-)
➜  merge-demo git:(master) git merge test
Auto-merging index.txt
CONFLICT (content): Merge conflict in index.txt
Automatic merge failed; fix conflicts and then commit the result.
➜  merge-demo git:(master) ✗ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   index.txt

no changes added to commit (use "git add" and/or "git commit -a")
➜  merge-demo git:(master) ✗ cat index.txt
<<<<<<< HEAD
third commit by master
=======
second commit by test
>>>>>>> test
➜  merge-demo git:(master) ✗ git checkout --ours index.txt
➜  merge-demo git:(master) ✗ cat index.txt
third commit by master
➜  merge-demo git:(master) ✗ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   index.txt

no changes added to commit (use "git add" and/or "git commit -a")
➜  merge-demo git:(master) ✗ git add -A && git commit -m 'done'
[master d2d2057] done