Git学习:冲突模拟一

Git学习

场景模拟

最小配置

用户 zlv11 本地创建一个git仓库,推送到远程仓库; 用户一:lvzb31988 , longzq

本地git仓库用户 lvzb31988

PS C:\Users\lvzb3> git config --global user.name 'lvzb31988'
PS C:\Users\lvzb3> git config --global user.email 'lvzb31988@163.com'
PS C:\Users\lvzb3> git config --global --list
user.name=lvzb31988
user.email=lvzb31988@163.com

远程git仓库用户 lvfiqfen

打通本地仓库到远程仓库

  1. 新建远程空仓库 gitbranchlearn, 本地创建空仓库 git_learn
  2. gitee官网 登录后,到【个人设置】->【数据管理】点击 【仓库空间信息】到右上角点击新建仓库,这里忽略SSH公钥管理步骤可参考生成/添加SSH公钥
  3. 到本地项目 执行 git init git_learn
  4. 绑定远程仓库和本地仓库: git remote add origin https://gitee.com/lvqifeng/gitbranchlearn
  5. 推送本地仓库代码到远程仓库
#绑定远程仓库和本地仓库
$ git remote add origin https://gitee.com/lvqifeng/gitbranchlearn

#推送本地仓库代码到远程仓库
$ git add -u
$ git commit -m "第一次提交"
$ git push origin master

远程仓库基于master分支创建新的分支 feature/git_add_commands

  • 本地git仓库克隆feature/git_add_commands分支 目录名称叫gitbranchlearn,该目录用于lvzb31988用户模拟第一个用户操作

    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp
    $ git clone git@gitee.com:lvqifeng/gitbranchlearn.git
    Cloning into 'gitbranchlearn'...
    remote: Enumerating objects: 14, done.
    remote: Counting objects: 100% (14/14), done.
    remote: Compressing objects: 100% (6/6), done.
    remote: Total 14 (delta 0), reused 0 (delta 0), pack-reused 0
    Receiving objects: 100% (14/14), done.
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp
    $ ll
    total 4
    drwxr-xr-x 1 lvzb3 197609 0 Oct  8 13:03 git_learn/
    drwxr-xr-x 1 lvzb3 197609 0 Oct  8 14:15 gitbranchlearn/
    
  • 本地git仓库克隆feature/git_add_commands分支 目录名称叫 gitbranchlearn_02,该目录用于 longzq用户模拟第二个用户操作

    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp
    $ git clone git@gitee.com:lvqifeng/gitbranchlearn.git  gitbranchlearn_02
    Cloning into 'gitbranchlearn_02'...
    remote: Enumerating objects: 14, done.
    remote: Counting objects: 100% (14/14), done.
    remote: Compressing objects: 100% (6/6), done.
    remote: Total 14 (delta 0), reused 0 (delta 0), pack-reused 0
    Receiving objects: 100% (14/14), done.
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp
    $ ll
    total 4
    drwxr-xr-x 1 lvzb3 197609 0 Oct  8 13:03 git_learn/
    drwxr-xr-x 1 lvzb3 197609 0 Oct  8 14:15 gitbranchlearn/
    drwxr-xr-x 1 lvzb3 197609 0 Oct  8 14:15 gitbranchlearn_02/
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (temp)
    $ pwd
    /c/Users/lvzb3/Desktop/temp/gitbranchlearn_02
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (temp)
    $ git config --local -l
    core.repositoryformatversion=0
    core.filemode=false
    core.bare=false
    core.logallrefupdates=true
    core.symlinks=false
    core.ignorecase=true
    remote.origin.url=git@gitee.com:lvqifeng/gitbranchlearn.git
    remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
    branch.temp.remote=origin
    branch.temp.merge=refs/heads/temp
    user.name=longzq
    user.email=Zhibiao.Lv@aexpec.com
    

用户 lvzb31988

  • 从远程仓库创建本地分支,同时用户 longzq 也创建本地分支。

    # 查看本地分支和远程分支,现在是处于本地 temp 分支(longzq拉取远程的分支就是)  
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn (temp)
    $ git branch -av
    * temp                                    e318862 temp 分支新增第四行
      remotes/origin/HEAD                     -> origin/temp
      remotes/origin/feature/add_git_commands e318862 temp 分支新增第四行
      remotes/origin/master                   51ecd33 rename a.txt to b.txt
      remotes/origin/temp                     e318862 temp 分支新增第四行
    
    # 基于远程仓库的feature/add_git_commands 分支创建本地分支 feature/add_git_commands 并切换到 feature/add_git_commands 分支
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn (temp)
    $ git checkout -b feature/add_git_commands origin/feature/add_git_commands
    Switched to a new branch 'feature/add_git_commands'
    Branch 'feature/add_git_commands' set up to track remote branch 'feature/add_git_commands' from 'origin'.
    
    # 这里可以看出 本地分支feature/add_git_commands 的 哈希码和 远程分支 /origin/feature/add_git_commands的是一致的均为:e318862
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn (feature/add_git_commands)
    $ git branch -av
    * feature/add_git_commands                e318862 temp 分支新增第四行
      temp                                    e318862 temp 分支新增第四行
      remotes/origin/HEAD                     -> origin/temp
      remotes/origin/feature/add_git_commands e318862 temp 分支新增第四行
      remotes/origin/master                   51ecd33 rename a.txt to b.txt
      remotes/origin/temp   
    
  • feature/add_git_commands创建分支下创建dd.txt文件 往里面添加内容 本地提交2次,同时推送到远程仓库

    • dd.txt 第一次添加内容,本地提交

      第一行新创建 dd.txt
      
      D:\IdeaProjects\temp\gitbranchlearn>git add dd.txt
      
      D:\IdeaProjects\temp\gitbranchlearn>git status
      On branch feature/add_git_commands
      Your branch is up to date with 'origin/feature/add_git_commands'.
      
      Changes to be committed:
        (use "git restore --staged <file>..." to unstage)
              new file:   dd.txt
      
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
              .idea/
      
      
      D:\IdeaProjects\temp\gitbranchlearn>git commit -am "dd.txt"
      [feature/add_git_commands d48c513] 'dd.txt'
       1 file changed, 1 insertion(+)
       create mode 100644 dd.txt
      
    • dd.txt 第二次添加内容,本地提交,并推送到远程仓库

      第一行新创建 dd.txt
      第二行追加内容 dd.txt
      
      D:\IdeaProjects\temp\gitbranchlearn>git status
      On branch feature/add_git_commands
      Your branch is ahead of 'origin/feature/add_git_commands' by 1 commit.
        (use "git push" to publish your local commits)
      
      Changes not staged for commit:
        (use "git add <file>..." to update what will be committed)
        (use "git restore <file>..." to discard changes in working directory)
              modified:   dd.txt
      
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
              .idea/
      
      no changes added to commit (use "git add" and/or "git commit -a")
      
      D:\IdeaProjects\temp\gitbranchlearn>git commit -am "add two line"
      [feature/add_git_commands c7edd89] add two line
       1 file changed, 2 insertions(+), 1 deletion(-)
      
      D:\IdeaProjects\temp\gitbranchlearn>git pull
      Already up to date.
      
      D:\IdeaProjects\temp\gitbranchlearn>git push
      Enumerating objects: 7, done.
      Counting objects: 100% (7/7), done.
      Delta compression using up to 16 threads
      Compressing objects: 100% (5/5), done.
      Writing objects: 100% (6/6), 562 bytes | 562.00 KiB/s, done.
      Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
      remote: Powered by GITEE.COM [GNK-6.4]
      To gitee.com:lvqifeng/gitbranchlearn.git
         9e92693..c7edd89  feature/add_git_commands -> feature/add_git_commands
      
  • 此时转到用户 longzq 进行对 dd.txt 进行操作,查看下面的用户 longzq

  • 回到这里此时用户 lvzb31988 本地的git仓库中dd.txt的内容还是

    第一行新创建 dd.txt
    第二行追加内容 dd.txtgit
    

    然后对lvzb31988用户对dd.txt文件追加一行后的内容,并提交到本地仓库和远程仓库此时就会出现冲突

    第一行新创建 dd.txt
    第二行追加内容 dd.txtgit
    第三行追加内容 lvzb31988
    
    D:\IdeaProjects\temp\gitbranchlearn>git commit -am "add line by lvzb31988"
    [feature/add_git_commands 6b7abef] add line by lvzb31988
     1 file changed, 2 insertions(+), 1 deletion(-)
    
    D:\IdeaProjects\temp\gitbranchlearn>git push
    To gitee.com:lvqifeng/gitbranchlearn.git
     ! [rejected]        feature/add_git_commands -> feature/add_git_commands (fetch first)
    error: failed to push some refs to 'gitee.com:lvqifeng/gitbranchlearn.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    
    

    这里提升要进行fetch操作,我们直接pull拉取一下,就会产生冲突,冲突内容

    <<<<<<< HEAD
    第一行新创建 dd.txt
    第二行追加内容 dd.txtgit
    第三行追加内容 lvzb31988
    =======
    第一行新创建 dd.txt        第一行被修改
    第二行追加内容 dd.txtgit   第二行被修改
    第三行是longzq用户添加的
    
    >>>>>>> 230ba5b19b86aabf7704527c57512c74739299af
    
    D:\IdeaProjects\temp\gitbranchlearn>git pull
    remote: Enumerating objects: 8, done.
    remote: Counting objects: 100% (8/8), done.
    remote: Compressing objects: 100% (6/6), done.
    remote: Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (6/6), 648 bytes | 40.00 KiB/s, done.
    From gitee.com:lvqifeng/gitbranchlearn
       c7edd89..230ba5b  feature/add_git_commands -> origin/feature/add_git_commands
    Auto-merging dd.txt
    CONFLICT (content): Merge conflict in dd.txt
    Automatic merge failed; fix conflicts and then commit the result.
    
    

    从文件里可以看出来 第一段 <<<<<< 到 ======== 之间的内容是用户 lvzb31988版本的, ======== 到 >>>>>>>> 之间的内容是用户longzq 版本的;

    如果用idea的话拉取就会弹出来一个 merge框进行合并操作图如下,左边的框就是我们本地的版本,最右侧的框就是远程仓库的版本,中间的框就是合并处理后我们的本地版本(在这个框里面进行冲突解决后如点击apply 会进行本地commit一次),根据具体情况进行合并解决冲突即可。
    在这里插入图片描述

    然后提交到远程仓库

    D:\IdeaProjects\temp\gitbranchlearn>git push
    Enumerating objects: 10, done.
    Counting objects: 100% (10/10), done.
    Delta compression using up to 16 threads
    Compressing objects: 100% (6/6), done.
    Writing objects: 100% (6/6), 648 bytes | 648.00 KiB/s, done.
    Total 6 (delta 4), reused 0 (delta 0), pack-reused 0
    remote: Powered by GITEE.COM [GNK-6.4]
    To gitee.com:lvqifeng/gitbranchlearn.git
       230ba5b..c19c7bc  feature/add_git_commands -> feature/add_git_commands
    

用户 longzq

  • 从远程仓库创建本地分支

    # 查看本地分支和远程分支,现在是处于本地 temp 分支(longzq拉取远程的分支就是)
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (temp)
    $ git branch -av
    * temp                                    e318862 temp 分支新增第四行
      remotes/origin/HEAD                     -> origin/temp
      remotes/origin/feature/add_git_commands e318862 temp 分支新增第四行
      remotes/origin/master                   51ecd33 rename a.txt to b.txt
      remotes/origin/temp                     e318862 temp 分支新增第四行
    
    # 查看本地分支
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (temp)
    $ git branch -v
    * temp e318862 temp 分支新增第四行
    
    # 基于远程仓库的feature/add_git_commands 分支创建本地分支 feature/add_git_commands 并切换到 feature/add_git_commands 分支
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (temp)
    $ git checkout -b feature/add_git_commands origin/feature/add_git_commands
    Switched to a new branch 'feature/add_git_commands'
    Branch 'feature/add_git_commands' set up to track remote branch 'feature/add_git_commands' from 'origin'.
    
    # 这里可以看出 本地分支feature/add_git_commands 的 哈希码和 远程分支 /origin/feature/add_git_commands的是一致的均为:e318862
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ git branch -av
    * feature/add_git_commands                e318862 temp 分支新增第四行
      temp                                    e318862 temp 分支新增第四行
      remotes/origin/HEAD                     -> origin/temp
      remotes/origin/feature/add_git_commands e318862 temp 分支新增第四行
      remotes/origin/master                   51ecd33 rename a.txt to b.txt
      remotes/origin/temp                     e318862 temp 分支新增第四行
    
    
  • 先拉取远程仓库,可以发现拉取前没有dd.txt,拉取后就有了 dd.txt

    ## 拉取前
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ ls
    b.txt  cc.txt
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ git pull
    remote: Enumerating objects: 11, done.
    remote: Counting objects: 100% (11/11), done.
    remote: Compressing objects: 100% (8/8), done.
    remote: Total 9 (delta 3), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (9/9), 813 bytes | 20.00 KiB/s, done.
    From gitee.com:lvqifeng/gitbranchlearn
       9e92693..c7edd89  feature/add_git_commands -> origin/feature/add_git_commands
       fe86d0e..d91ada6  master     -> origin/master
       fe86d0e..867bf4c  temp       -> origin/temp
    Updating 9e92693..c7edd89
    Fast-forward
     dd.txt | 2 ++
     1 file changed, 2 insertions(+)
     create mode 100644 dd.txt
     
    ## 拉取后
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ ls
    b.txt  cc.txt  dd.txt
    
    
  • dd.txt 追加内容, 并且提交到本地仓库

    第三行是longzq用户添加的
    
    $ cat dd.txt
    第一行新创建 dd.txt
    第二行追加内容 dd.txtgit
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ vim dd.txt
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ cat dd.txt
    第一行新创建 dd.txt
    第二行追加内容 dd.txtgit
    第三行是longzq用户添加的
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ git commit -am 'add three line'
    [feature/add_git_commands 3b52527] add three line
     1 file changed, 3 insertions(+), 1 deletion(-)
    
    
  • 修改dd.txt 内容,并且提交到远程仓库

    第一行新创建 dd.txt        第一行被修改
    第二行追加内容 dd.txtgit   第二行被修改
    第三行是longzq用户添加的
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ cat dd.txt
    第一行新创建 dd.txt
    第二行追加内容 dd.txtgit
    第三行是longzq用户添加的
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ cat dd.txt
    第一行新创建 dd.txt        第一行被修改
    第二行追加内容 dd.txtgit   第二行被修改
    第三行是longzq用户添加的
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ git commit -am'update one,two line'
    [feature/add_git_commands 230ba5b] update one,two line
     1 file changed, 2 insertions(+), 2 deletions(-)
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ git pull
    Already up to date.
    
    lvzb3@LAPTOP-3E5DGGO8 MINGW64 ~/Desktop/temp/gitbranchlearn_02 (feature/add_git_commands)
    $ git push
    Enumerating objects: 8, done.
    Counting objects: 100% (8/8), done.
    Delta compression using up to 16 threads
    Compressing objects: 100% (6/6), done.
    Writing objects: 100% (6/6), 668 bytes | 668.00 KiB/s, done.
    Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
    remote: Powered by GITEE.COM [GNK-6.4]
    To gitee.com:lvqifeng/gitbranchlearn.git
       c7edd89..230ba5b  feature/add_git_commands -> feature/add_git_commands
    
  • 切换到lvzb31988用户,这里假设 lvzb31988不知道 longzq已经提交了,然后 lvzb31988本地还在修改中,2个用户同时修改同一个文件,就会产生冲突

冲突演示小结

  • 每次提交前先拉取代码,尽量及时提交代码,避免版本差异过大,解决繁琐的冲突
  • 遇到冲突时,根据具体情况进行合并,然后及时提交

这里再通过 git的版本节点图进行说明:

用户 lvzb31988 提交了 dd.txt, 并且在里面添加了2行记录 提交到远程仓库。

用户 longzq 从远程仓库拉取了 前面 lvzb31988的操作,并且对dd.txt内容进行了修改提交了2次本地提交,提交到远程仓库。在这期间,用户lvzb31988 又对dd.txt进行了修改并且提交到了远程仓库此时,lvzb31988的本地git仓库内容和远程仓库的内容已经不一致,因此 lvzb31988在提交本地仓库到远程仓库的时候就会有冲突产生,lvzb31988在提交前拉取了一下远程仓库,遇到冲突进行合并。合并后本git仓库再提交一次,再推送到远程仓库。

在这里插入图片描述

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值