Git 分支合并冲突及合并分类
  TEZNKK3IfmPf 2023年11月14日 67 0
git

Git 分支合并冲突及合并分类

  1. 分支合并冲突
##创建分支datagrand
git checkout -b datagrand
   Switched to a new branch 'datagrand'
git branch -a
  master
\* datagrand
##在datagrand分支上创建文件
echo "this is git file!" > read.txt
git add read.txt
git commit -m "add a new file"
git push -u origin datagrand
##切换到master分支,建立read.txt文件
echo "my name is wtf" > read.txt
git add read.txt
git commit -m "add a new file"
git push 
##合并分支
##在master分支上操作
git merge datagrand
##说明:这种情况下,Git无法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,报错如下:
Auto-merging read.txt
CONFLICT (add/add): Merge conflict in read.txt
Automatic merge failed; fix conflicts and then commit the result.
##说明:Git告诉我们read.txt文件存在冲突,必须手动解决冲突后再提交。
  1. 解决合并冲突
##查看下当前分支状态
git status
On branch master
Your 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 <file>..." to mark resolution)
  both added:      read.txt
no changes added to commit (use "git add" and/or "git commit -a")
##查看一下readme.txt中的内容
cat read.txt
<<<<<<< HEAD
my name is wtf!
=======
this is git file!
\>>>>>>> datagrand
##说明:Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,让我们选择要保留的内容,下面我们修改一下readme.txt,再次提交。
##操作步骤如下:
vim read.txt
git add read.txt
git commit -m "fixed"
[master 935e613] fixed
git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean
cat read.txt
this is git file!
##分支合并
git merge datagrand
##查看合并后状态
git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean
  1. 删除分支
git branch -d datagrand
Deleted 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
|/
  1. 合并分支(普通合并) 分支合并分为快速合并与普通合并两种模式,普通合并,合并后的历史有分支记录,能看出来曾经做过合并,而快速合并就看不出来曾经做过合并。下面我们来演示一下普通合并:
##新创建分支grand
git checkout -b grand
##在grand分支上创建文件
echo "this is a test file" > readme.txt
##git操作
git add readme.txt
git 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合并就看不出来曾经做过合并。
  1. 快速合并与普通合并的比较 (1)快速合并 (2)普通合并
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年04月12日   54   0   0 命令git
  TEZNKK3IfmPf   2024年04月19日   65   0   0 git部署
  TEZNKK3IfmPf   2024年04月26日   40   0   0 gitgithub
TEZNKK3IfmPf