➜ mygit git:(new_branch) git checkout - Switched to branch 'master' ➜ mygit git:(master) git branch -d new_branch error: The branch 'new_branch' is not fully merged. If you are sure you want to delete it, run 'git branch -D new_branch'.
➜ mygit git:(master) git checkout -b new_branch4 Switched to a new branch 'new_branch4'
合并
刚才创建分支后删除,有提示到说没有合并,我们再创建一个分支,并对文件做一些修改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
➜ mygit git:(new_branch4) vi test.txt ➜ mygit git:(new_branch4) ✗ git status On branch new_branch4 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)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a") ➜ mygit git:(new_branch4) ✗ git add . ➜ mygit git:(new_branch4) ✗ git commit -m 'add a new line in test' [new_branch4 41c3327] add a new line intest 1 file changed, 1 insertion(+) ➜ mygit git:(new_branch4) git checkout - Switched to branch 'master' ➜ mygit git:(master) git branch * master new_branch4
➜ mygit git:(master) git checkout -b dev Switched to a new branch 'dev' ➜ mygit git:(dev) cat .git/HEAD ref: refs/heads/dev
这张图又什么意思呢,相信很容易猜到了,就是在dev分支上产生了一次提交。
1 2 3 4 5
➜ mygit git:(dev) vi test.txt ➜ mygit git:(dev) ✗ git add . ➜ mygit git:(dev) ✗ git commit -m 'add a new line in test.txt' [dev bd1bf5d] add a new line in test.txt 1 file changed, 1 insertion(+)
➜ mygit git:(master) git merge dev Auto-merging test2.txt CONFLICT (add/add): Merge conflict in test2.txt Automatic merge failed; fix conflicts and then commit the result.
出现了冲突,git不知道如何处理这种冲突,需要我们自己解决,我们现在看一下test2.txt的内容是什么 vi test2.txt
1 2 3 4 5
<<<<<<< HEAD hello java ======= hello kotlin >>>>>>> dev
➜ mygit git:(master) ✗ vi test2.txt ➜ mygit 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 added: test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
现在执行git add test2.txt表示冲突已经解决,可以提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
➜ mygit git:(master) ✗ git add test2.txt ➜ mygit git:(master) ✗ git status On branch master All conflicts fixed but you are still merging. (use "git commit" to conclude merge)
Changes to be committed:
modified: test2.txt
➜ mygit git:(master) ✗ git commit [master b4bd64b] Merge branch 'dev' ➜ mygit git:(master) git status On branch master nothing to commit, working tree clean ➜ mygit git:(master) cat test2.txt hello java hello kotlin
现在再回过头来看看dev分支下test2.txt的内容:
1 2 3 4
➜ mygit git:(master) git checkout dev Switched to branch 'dev' ➜ mygit git:(dev) cat test2.txt hello kotlin
ca07f7b (HEAD -> master) HEAD@{0}: reset: moving to ca07f7b 0d2246f HEAD@{1}: reset: moving to HEAD 0d2246f HEAD@{2}: reset: moving to HEAD 0d2246f HEAD@{3}: reset: moving to HEAD^^ 3240097 HEAD@{4}: reset: moving to HEAD^ ca07f7b (HEAD -> master) HEAD@{5}: commit: add another line 3240097 HEAD@{6}: commit: add another line d8f57a9 HEAD@{7}: commit: add a new line 0d2246f HEAD@{8}: commit (initial): initial commit (END)