美文网首页
Git命令 - git合并冲突

Git命令 - git合并冲突

作者: BlueSocks | 来源:发表于2023-07-19 13:49 被阅读0次

1. 冲突产生

  1. 首先我们创建本地git仓库,并添加test.txt文件,如下:
$ git init
//创建默认分支
$ git switch --create main
$ touch test.txt
$ git add test.txt
$ git commit -m "Initial commit"

  1. 创建开发分支dev
$ git checkout main
$ git checkout -b dev

dev 分支修改test.txt,内容如下:

Hello

然后提交

$ git add test.txt
$ git commit -m "修改test.txt"

  1. 切 回到main分支,修改test.txt 的内容为Hello World ,然后提交:
$ git checkout main
//修改test.txt文件,内容如下
Hello World
//然后提交
$ git add test.txt
$ git commit -m "修改test.txt"

  1. 合并dev分支

此时,main分支dev分支都修改了test.txt文件,然后合并dev分支到main分支,Git告诉我们,test.txt文件存在冲突,必须手动解决冲突后再提交

$ git merge dev
//输出日志如下
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result. 

  1. 使用git status可以查看冲突的文件
On branch main
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:   test.txt

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

2. 解决合并冲突

查看冲突文件test.txt,Git用<<<<<<<=======>>>>>>>标记出不同分支的内容,内容如下:

<<<<<<< HEAD
Hello World
=======
Hello
>>>>>>> dev

此时,就可以根据情况选择test.txt保留的内容。现在我们修改test.txt,保留Hello World,内容如下:

Hello World

再提交

$ git add test.txt
$ git commit -m "解决冲突"

此时,再使用git status命令查看,发现已经没有冲突提示了。

3. 关于合并冲突

在git中,出现冲突的场景有很多,git merge、git pull、git rebase等命令都有可能出现冲突,但是他们的本质是相同的:在合并文件时同一个文件的同一个位置都修改了,并且内容不同;也即两个已经提交的分支的相同文件相同位置的不同操作进行了合并,所以产生了冲突。 然后git会提示

Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.

如何解决冲突?就需要查看冲突的文件,修改冲突内容,然后重新提交。

相关文章

  • git分支合并及切换

    git分支合并使用 git merge branchname 命令,若提示有冲突,先手动修改冲突后保存提交 git...

  • (二)

    转自:Android Studio、Git 解决合并冲突Android Studio、Git 解决合并冲突Andr...

  • Git分支合并及冲突解决

    分支合并 $ git checkout 分支名A $ git merge 分支名B 产生合并冲突 查看冲突情况 $...

  • git命令(push、pull、branch、tag)

    git命令 记录git相关的命令 创建/切换/合并 分支 创建/查看tag 当远程仓库和本地仓库内容又冲突时(多人...

  • SVN 部分命令

    1. 从版本控制删除文件 2. git svn 命令 当我们使用“git svn rebase”合并产生冲突是,我...

  • 如何正确rebase

    git rebase -i origin/分支 合并冲突 git status git rebase --cont...

  • (4.2) git分支管理-解决冲突

    小结 当Git无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。 解决冲突就是把Git合并失败...

  • Git 14合并分支

    合并分支 ========= 命令 git merge [name] 合并分支 分支名称 git branch -...

  • 【Git】分支合并工具使用

    Git 预设了许多其他的合并和解决冲突的工具,通过下面这个命令可以查看: 下面说明Mac上安装Git的合并工具P4...

  • Git 系列

    Git 合并冲突 $ git pull error: Your local changes to the foll...

网友评论

      本文标题:Git命令 - git合并冲突

      本文链接:https://www.haomeiwen.com/subject/qnooudtx.html