本文共 2594 字,大约阅读时间需要 8 分钟。
Git 分支合并冲突及合并分类
##创建分支datagrandgit checkout -b datagrandSwitched to a new branch 'datagrand'git branch -amaster\* datagrand##在datagrand分支上创建文件echo "this is git file!" > read.txtgit add read.txtgit commit -m "add a new file"git push -u origin datagrand##切换到master分支,建立read.txt文件echo "my name is wtf" > read.txtgit add read.txtgit commit -m "add a new file"git push ##合并分支##在master分支上操作git merge datagrand##说明:这种情况下,Git无法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,报错如下:Auto-merging read.txtCONFLICT (add/add): Merge conflict in read.txtAutomatic merge failed; fix conflicts and then commit the result.##说明:Git告诉我们read.txt文件存在冲突,必须手动解决冲突后再提交。
##查看下当前分支状态git statusOn branch masterYour branch is up-to-date with 'origin/master'.You have unmerged paths.(fix conflicts and run "git commit")(use "git merge --abort" to abort the merge)Unmerged paths:(use "git add..." to mark resolution)both added: read.txtno changes added to commit (use "git add" and/or "git commit -a")##查看一下readme.txt中的内容cat read.txt<<<<<<< HEADmy name is wtf!=======this is git file!\>>>>>>> datagrand##说明:Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,让我们选择要保留的内容,下面我们修改一下readme.txt,再次提交。##操作步骤如下:vim read.txtgit add read.txtgit commit -m "fixed"[master 935e613] fixedgit statusOn branch masterYour branch is ahead of 'origin/master' by 2 commits.(use "git push" to publish your local commits)nothing to commit, working tree cleancat read.txtthis is git file!##分支合并git merge datagrand##查看合并后状态git statusOn branch masterYour branch is ahead of 'origin/master' by 2 commits.(use "git push" to publish your local commits)nothing to commit, working tree clean
git branch -d datagrandDeleted branch datagrand (was 3299654).##查看一下分支合并信息git log --graph --pretty=oneline --abbrev-commit \* 935e613 (HEAD -> master) fixed|\| * 3299654 (origin/datagrand) add a new file\* | d4f781c (origin/master) add a file read\* | ddb3e06 Delete read.txt\* | 13bfb1c add a file read|/
##新创建分支grandgit checkout -b grand##在grand分支上创建文件echo "this is a test file" > readme.txt##git操作git add readme.txtgit commit -m "add merge a line"##切换到master分支git checkout master##普通合并git merge --no-ff -m "merge with no-ff" grand说明:--no-ff参数表示禁用快速合并##查看合并历史git log --graph --pretty=oneline --abbrev-commit\* 96a332a (HEAD -> master) merge with no-ff|\| * 347b403 (grand) add merge a line|/说明:合并分支时,加上--no-ff参数就可以用普通模式合并,合并后的历史有分支记录,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并。
转载于:https://blog.51cto.com/wutengfei/2091410