其他分享
首页 > 其他分享> > 02 《不同人修改了同文件的不同区域如何处理》-2022.4.23(博客分享)

02 《不同人修改了同文件的不同区域如何处理》-2022.4.23(博客分享)

作者:互联网

2 《不同人修改了同文件的不同区域如何处理》-2022.4.23(博客分享)

image-20220423065736365

目录

目录

实验环境

$ git version
git version 2.17.0.windows.1

1、问题背景

不同人修改了同文件的不同区域如何处理?

2、测试过程

1️⃣ 2个用户先来同步下当前仓库的状态(使用git pull目录拉取下仓库)

1、xyy用户默认已经是最新的了!

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git branch  -av
* feature/add_git_commands                4e1f688 Merge remote-tracking branch 'github/feature/add_git_commands' into feature/add_git_commands
  master                                  b1c97fa Merge remote-tracking branch 'github/main'
  suling                                  30c1501 Add test
  temp                                    30c1501 Add test
  remotes/github/feature/add_git_commands 4e1f688 Merge remote-tracking branch 'github/feature/add_git_commands' into feature/add_git_commands
  remotes/github/main                     498eb05 Initial commit
  remotes/github/master                   7b84beb Add the first command with config
  remotes/github/suling                   30c1501 Add test
  remotes/github/temp                     30c1501 Add test
  remotes/zhineng/suling                  30c1501 Add test

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git pull
Already up to date.

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git branch  -av
* feature/add_git_commands                4e1f688 Merge remote-tracking branch 'github/feature/add_git_commands' into feature/add_git_commands
  master                                  b1c97fa Merge remote-tracking branch 'github/main'
  suling                                  30c1501 Add test
  temp                                    30c1501 Add test
  remotes/github/feature/add_git_commands 4e1f688 Merge remote-tracking branch 'github/feature/add_git_commands' into feature/add_git_commands
  remotes/github/main                     498eb05 Initial commit
  remotes/github/master                   7b84beb Add the first command with config
  remotes/github/suling                   30c1501 Add test
  remotes/github/temp                     30c1501 Add test
  remotes/zhineng/suling                  30c1501 Add test

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)

2、xyy02用户拉取仓库最新数据

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ git branch  -av
* feature/add_git_commands                1059f72 Fix deadme
  main                                    498eb05 Initial commit
  remotes/origin/HEAD                     -> origin/main
  remotes/origin/feature/add_git_commands 1059f72 Fix deadme
  remotes/origin/main                     498eb05 Initial commit
  remotes/origin/master                   7b84beb Add the first command with config
  remotes/origin/suling                   30c1501 Add test
  remotes/origin/temp                     30c1501 Add test

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ git pull
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 10 (delta 2), reused 10 (delta 2), pack-reused 0
Unpacking objects: 100% (10/10), done.
From github.com:OnlyOnexl/git_learning
   1059f72..4e1f688  feature/add_git_commands -> origin/feature/add_git_commands
Updating 1059f72..4e1f688
Fast-forward
 doc/test.txt | 1 +
 gitignore    | 1 +
 images/doc   | 0
 index.html   | 2 +-
 4 files changed, 3 insertions(+), 1 deletion(-)
 create mode 100644 doc/test.txt
 create mode 100644 gitignore
 create mode 100644 images/doc

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ git branch  -av
* feature/add_git_commands                4e1f688 Merge remote-tracking branch 'github/feature/add_git_commands' into feature/add_git_commands
  main                                    498eb05 Initial commit
  remotes/origin/HEAD                     -> origin/main
  remotes/origin/feature/add_git_commands 4e1f688 Merge remote-tracking branch 'github/feature/add_git_commands' into feature/add_git_commands
  remotes/origin/main                     498eb05 Initial commit
  remotes/origin/master                   7b84beb Add the first command with config
  remotes/origin/suling                   30c1501 Add test
  remotes/origin/temp                     30c1501 Add test

2️⃣ 模拟xyy和xyy02这2个用户同时修改index.html文件不同区域

1.xyy用户添加一些git术语到index.html文件

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ vim index.html 

image-20220418062251707

先进行提交:

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git commit -am"Add non fast-forward"
[feature/add_git_commands be06ac0] Add non fast-forward
 1 file changed, 1 insertion(+), 1 deletion(-)
 
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 310 bytes | 34.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:OnlyOnexl/git_learning.git
   4e1f688..be06ac0  feature/add_git_commands -> feature/add_git_commands

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ gs
On branch feature/add_git_commands
Your branch is up to date with 'github/feature/add_git_commands'.

nothing to commit, working tree clean

2.xyy02用户添加一个git命令到index.html文件

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ vi index.html 

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ git commit -am"Add commit command"
[feature/add_git_commands 5ea4a6f] Add commit command
 1 file changed, 1 insertion(+), 1 deletion(-)

此时用户xyy02并不知道仓库已经发生了更新,此时它要推送,看下会发生什么情况?

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ git push
To github.com:OnlyOnexl/git_learning.git
 ! [rejected]        feature/add_git_commands -> feature/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:OnlyOnexl/git_learning.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.

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ git branch -av
* feature/add_git_commands                5ea4a6f [ahead 1] Add commit command
  main                                    498eb05 Initial commit
  remotes/origin/HEAD                     -> origin/main
  remotes/origin/feature/add_git_commands 4e1f688 Merge remote-tracking branch 'github/feature/add_git_commands' into feature/add_git_commands
  remotes/origin/main                     498eb05 Initial commit
  remotes/origin/master                   7b84beb Add the first command with config
  remotes/origin/suling                   30c1501 Add test
  remotes/origin/temp                     30c1501 Add test

我们会发现,此时远端仓库比我们本地分支[ahead 1] ,不属于fast-forward关系,因此推送失败!

3️⃣ 因此,xyy02用户该如何解决呢?

有2种方法:

  1. 先fetch,再merge

  2. 先pull再push

这里我们先使用第一种方法:

先执行git fetch命令,会发现当前本地分支feature/add_git_commands的提示为[ahead 1, behind 1]

image-20220418063915817

此时我们执行git merge命令,将远程分支'origin/feature/add_git_commands' 合并到本地分支 feature/add_git_commands:

image-20220418064227674

我们再通过$ gitk --all命令看下版本树关系:

image-20220418064357826

最后,xyy02用户直接执行git push命令,并查看当前all分支,会发现本地分支和远程分支的commit id是一样的:

image-20220418064542233

再次查看版本树情况,发现本地分支和远程分支已经指向同一个地方了:

hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ gitk --all

image-20220418064725381

再看下index.html文件的修改状态信息:

image-20220418064913231

3、总结

推荐:一般先使用git pull命令,再使用git push即可!

image-20220418065625401

关于我

我的博客主旨:我希望每一个人拿着我的博客都可以做出实验现象,先把实验做出来,然后再结合理论知识更深层次去理解技术点,这样学习起来才有乐趣和动力。并且,我的博客内容步骤是很完整的,也分享源码和实验用到的软件,希望能和大家一起共同进步!

各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人免费帮您解决问题:

  1. 个人微信二维码:x2675263825 (舍得), qq:2675263825。

    image-20211002091450217

  2. 个人微信公众号:云原生架构师实战

    image-20211002141739664

  3. 个人博客地址:www.onlyonexl.cn

    image-20211002092057988

  4. 个人csdn

    https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

    image-20211002092344616

  5. 个人已开源干货

    标签:02,commands,git,remotes,23,feature,Add,add,2022.4
    来源: https://www.cnblogs.com/520ZXL/p/16181375.html