9.git分支合并

git分支合并

提交到新分支上

git  add .
git  commit -m 'beike-dev'
git  push -u origin dev

将dev分支合并master

//1.首先切换到master分支上
git  checkout master

//2.更新远程分支
git pull origin master

//3.将dev分支的代码合并到master上
git  merge  dev
//查看状态及执行提交命令
git status

On branch master
Your branch is ahead of 'origin/master' by 12 commits.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

//你有12个commit,需要push到远程master上 
//最后执行下面提交命令
git push origin master

上传远程分支

git push origin master

切换回原开发分支

git checkout dev

git merge

合并
合并之前先要到合并后的 分支上
例如 将dedug分支 合并到 master上

git checkout master
git merge debug

合并冲突要手动合并解决

合并冲突

冲突提示:等号上边时当前分支代码,下边时合并分支的代码

Git用<<<<<<<=======>>>>>>>标记出不同分支的内容

<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> dev

冲突案例

假设readme.txt文件存在冲突,必须手动解决冲突后再提交。

$ git merge dev
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

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:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

查看冲突文件

<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> dev

将上面冲突修改为

Creating a new branch is quick and simple.

再次提交合并

$ git add readme.txt 
$ git commit -m "conflict fixed"
[master cf810e4] conflict fixed
$ git log --graph --pretty=oneline --abbrev-commit
*   cf810e4 (HEAD -> master) conflict fixed
|\  
| * 14096d0 (dev) AND simple
* | 5dc6824 & simple
|/  
* b17d20e branch test
* d46f35e (origin/master) remove test.txt
* b84166e add test.txt
* 519219b git tracks changes
* e43a48b understand how stage works
* 1094adb append GPL
* e475afc add distributed
* eaadf4e wrote a readme file

参考资料

https://zhuanlan.zhihu.com/p/47842234

https://blog.csdn.net/bufegar0/article/details/109597640

posted @ 2023-10-26 09:07  贝壳里的星海  阅读(64)  评论(0编辑  收藏  举报