创建新的分支 $ git checkout -b dev // Switched to a new branch 'dev' 74565@jason MINGW64 /c/notos/code/gitpractice (dev) $ git branch //查看git下所有分支 ,*表示当前分支 * dev master git checkout -b dev 这条命令可以用一下两条命令替代 $ git branch dev //创建分支 $ git checkout dev //切换分支
合并分支
//在新分支上修改文件并提交 $ git add readme.txt 74565@jason MINGW64 /c/notos/code/gitpractice (dev) $ git commit -m "branch test" [dev 84a8f86] branch test 1 file changed, 1 insertion(+) $ git checkout master //首先切换回master分支 Already on 'master' Your branch is up-to-date with 'origin/master'. 74565@jason MINGW64 /c/notos/code/gitpractice (master) $ git branch dev * master 74565@jason MINGW64 /c/notos/code/gitpractice (master) $ git merge dev // 合并得知 分支到当前分支 git merge --no-ff -m "merge with no-ff" dev 采用非fast-forward方式合并分支 Updating 4b00677..84a8f86 Fast-forward readme.txt | 1 + 1 file changed, 1 insertion(+)
删除分支
$ git branch -d dev //删除分支 Deleted branch dev (was 84a8f86). 74565@jason MINGW64 /c/notos/code/gitpractice (master) $ git branch * master
解决冲突
假如在一个新的分支上修改了readme并进行提交,master上也对readme进行了修改,而且修改的是同一行,也commit了,那么合并新分支到master时会报错 $ git merge feature1 Auto-merging readme.txt CONFLICT (content): Merge conflict in readme.txt Automatic merge failed; fix conflicts and then commit the result.//自动合并失败,手动解决冲突后,重新提交 手动解决冲突航班重新提交 git add readme.txt 74565@jason MINGW64 /c/notos/code/gitpractice (master|MERGING) $ git commit -m "fixed conflict" [master 4cdbdc4] fixed conflict