$ gc -a -m "test 0" [master 9a38a47] test 0 1 file changed, 1 insertion(+)
切换到 test 分支,修改同一行代码,这样合并的时候就会产生冲突,进行一次提交,如下:
1 2 3 4
$ gc -a -m "test 1" [test 0c21bb9] test 1 1 file changed, 1 insertion(+)
在另一个方法中添加一行,再进行一次提交,如下:
1 2 3
$ gc -a -m "test 2" [test 54d3bd2] test 2 1 file changed, 1 insertion(+), 1 deletion(-)
把 test 分支合并到 master 分支,产生冲突,如下:
1 2 3 4
$ git merge test Auto-merging CPYMergeDemo/ViewController.m CONFLICT (content): Merge conflict in CPYMergeDemo/ViewController.m Automatic merge failed; fix conflicts and then commit the result.
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. <<<<<<< HEAD NSLog(@"0"); ======= NSLog(@"1"); >>>>>>> test }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. NSLog(@"2"); }
@end
合并产生冲突后,会在 .git 目录产生几个记录此次 merge 相关的标记文件,如下:
1 2 3
$ ls .git COMMIT_EDITMSG MERGE_HEAD MERGE_MSG config hooks/ info/ objects/ HEAD MERGE_MODE ORIG_HEAD description index logs/ refs/
暂存区的情况变成如下:
1 2 3 4 5 6 7 8 9 10 11 12
$ 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: CPYMergeDemo/ViewController.m
no changes added to commit (use "git add" and/or "git commit -a")
若想丢弃此次合并,可以使用命令重置,回到当前分支的 HEAD,命令如下:
1
git reset --hard HEAD
此时 git 状态回到合并前,如下:
1 2
$ ls .git COMMIT_EDITMSG HEAD config description hooks/ index info/ logs/ objects/ refs/
暂存区里没有更改:
1 2 3
$ git status On branch master nothing to commit, working tree clean
但是使用 Xcode 的 Source Control 中的 Discard All Changes… 操作重置后,暂存区变成如下所示,显示出有一个 merge 正在进行。
1 2 3 4
$ git status On branch test_1 All conflicts fixed but you are still merging. (use "git commit" to conclude merge)
git 的状态还保留了合并未完成的状态,如下:
1 2 3
$ ls .git COMMIT_EDITMSG MERGE_HEAD MERGE_MSG config hooks/ info/ objects/ HEAD MERGE_MODE ORIG_HEAD description index logs/ refs/
$ git status On branch master All conflicts fixed but you are still merging. (use "git commit" to conclude merge)
Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)