git:不同人修改了不同文件如何处理
git:不同人修改了不同文件如何处理目录文章目录git:不同人修改了不同文件如何处理目录实验环境实验背景1、在github远程仓库创建一个需要2个人共同开发的新分支2、本地第2个开发者`xyy02`克隆这个仓库修改并进行提交代码3、本地第1个开发者`xyy`修改并进行提交代码注意事项📍 github缺省分支关于我最后实验环境win10$ git versiongit version 2.17.0
git:不同人修改了不同文件如何处理
目录
文章目录
实验环境
win10
$ git version
git version 2.17.0.windows.1
实验背景
这2个人是维护同一个分支的,有2个git客户端,然后这2个人修改了不一样的文件,模拟2个账号,进行git测试!
1、在github远程仓库创建一个需要2个人共同开发的新分支
我们这里基于master分支创建一个新分支feature/add_git_commands
:
https://github.com/OnlyOnexl/git_learning
2、本地第2个开发者xyy02
克隆这个仓库修改并进行提交代码
1️⃣ 克隆远程仓库到本地:
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit
$ ls
666-bakup/ Git-Learning-master.zip git_learning/ testGit/
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit
$ git clone git@github.com:OnlyOnexl/git_learning.git
fatal: destination path 'git_learning' already exists and is not an empty directory.
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit
$ git clone git@github.com:OnlyOnexl/git_learning.git git_learning_02 #注意,这种方式!
Cloning into 'git_learning_02'...
remote: Enumerating objects: 31, done.
remote: Counting objects: 100% (31/31), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 31 (delta 6), reused 27 (delta 6), pack-reused 0
Receiving objects: 100% (31/31), 23.28 KiB | 410.00 KiB/s, done.
Resolving deltas: 100% (6/6), done.
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit
$ ls
666-bakup/ Git-Learning-master.zip git_learning/ git_learning_02/ testGit/
2️⃣ 进入仓库,并设置签名:
这里设置为项目级别签名,其优先级高于全局优先级:
我们先来查看下原来本地仓库的签名:
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (master)
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[user]
name = xyy
email = xyy@189.cn
[gui]
wmstate = normal
geometry = 1061x563+96+96 233 255
[remote "zhineng"]
url = file:///c/Users/hg/Desktop/it/01-SuGit/666-bakup/zhineng.git
fetch = +refs/heads/*:refs/remotes/zhineng/*
[branch "suling"]
remote = zhineng
merge = refs/heads/suling
[remote "github"]
url = git@github.com:OnlyOnexl/git_learning.git
fetch = +refs/heads/*:refs/remotes/github/*
这里我们再设置下用户xyy的签名:
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (main)
$ pwd
/c/Users/hg/Desktop/it/01-SuGit/git_learning_02
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (main) #设置项目级别签名,其优先级高于全局配置
$ git config --local user.name "xyy02"
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (main)
$ git config --local user.email "xyy02@189.cn"
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (main)
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = git@github.com:OnlyOnexl/git_learning.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
[user]
name = xyy02
email = xyy02@189.cn
3️⃣ 基于远程分支创建本地新分支
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (main)
#查看当前仓库所有分支
$ git branch -av
* main 498eb05 Initial commit
remotes/origin/HEAD -> origin/main
remotes/origin/feature/add_git_commands 7b84beb Add the first command with config
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 (main)
#基于远程分支创建本地新分支
$ 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'.
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
4️⃣ 用户xyy02修改readme文件并推送到远程仓库
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ echo "We are going to record some git commands here." >> readme
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ git add -A
warning: LF will be replaced by CRLF in readme.
The file will have its original line endings in your working directory.
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ git commit -m"Add git commands description in readme"
[feature/add_git_commands 694cc4c] Add git commands description in readme
1 file changed, 1 insertion(+)
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ gs
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)
nothing to commit, working tree clean
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (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), 326 bytes | 108.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:OnlyOnexl/git_learning.git
7b84beb..694cc4c feature/add_git_commands -> feature/add_git_commands
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ gs
On branch feature/add_git_commands
Your branch is up to date with 'origin/feature/add_git_commands'.
nothing to commit, working tree clean
5️⃣ 查看github仓库feature/add_git_commands
分支下readme文件是否发生改变
已经发生改变,符合预期。
3、本地第1个开发者xyy
修改并进行提交代码
🍀 查看当前环境
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit
$ cd git_learning
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (master)
$ git branch -av
* master b1c97fa Merge remote-tracking branch 'github/main'
suling 30c1501 Add test
temp 30c1501 Add test
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
🍀 通过git fetch命令将远程仓库有变更的信息拉取到本地
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (master)
$ git fetch github
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:OnlyOnexl/git_learning
* [new branch] feature/add_git_commands -> github/feature/add_git_commands
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (master)
$ git branch -av
* master b1c97fa Merge remote-tracking branch 'github/main'
suling 30c1501 Add test
temp 30c1501 Add test
remotes/github/feature/add_git_commands 694cc4c Add git commands description in readme
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
🍀 基于远程仓库github/feature/add_git_commands
分支创建本地分支feature/add_git_commands
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (master)
$ git checkout -b feature/add_git_commands github/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 'github'.
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git branch -av
* feature/add_git_commands 694cc4c Add git commands description in readme
master b1c97fa Merge remote-tracking branch 'github/main'
suling 30c1501 Add test
temp 30c1501 Add test
remotes/github/feature/add_git_commands 694cc4c Add git commands description in readme
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
🍀 用户xyy修改index.html文件并提交
$ vim index.html
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git add -A
warning: LF will be replaced by CRLF in doc/test.txt.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in gitignore.
The file will have its original line endings in your working directory.
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git commit -m"Add git command in index.html"
[feature/add_git_commands 1c03316] Add git command in index.html
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 (feature/add_git_commands)
$ gs
On branch feature/add_git_commands
Your branch is ahead of 'github/feature/add_git_commands' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
🍀 gitk --all
如果现在使用xyy这个账号直接推送的话,因为根据版本树关系,本地feature/add_git_commands分支和github/feature/add_git_commands分支是fast-forwards关系,因此直接推送是没任何问题的。
但是,我们目前模拟下如下这种情况:
我们先利用xyy02这个账号,修改下文件,然后推送到仓库。
此时,我们再以xyy用户进行推送刚才做的提交,看是否有情况发生?
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ cd ../git_learning_02/
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ git branch -av
* feature/add_git_commands 694cc4c Add git commands description in readme
main 498eb05 Initial commit
remotes/origin/HEAD -> origin/main
remotes/origin/feature/add_git_commands 694cc4c Add git commands description in readme
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
# 修改readme文件
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ vim readme
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ cat readme
i love you,xyy!
We are going to record some git commands here.eg add and so on
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ git add -A
warning: LF will be replaced by CRLF in readme.
The file will have its original line endings in your working directory.
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ git commit -m"Fix deadme"
[feature/add_git_commands 1059f72] Fix deadme
1 file changed, 1 insertion(+), 1 deletion(-)
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (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), 315 bytes | 105.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:OnlyOnexl/git_learning.git
694cc4c..1059f72 feature/add_git_commands -> feature/add_git_commands
🍀 此时来到xyy账户进行推送
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning_02 (feature/add_git_commands)
$ cd ../git_learning
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git branch -av
* feature/add_git_commands 1c03316 [ahead 1] Add git command in index.html
master b1c97fa Merge remote-tracking branch 'github/main'
suling 30c1501 Add test
temp 30c1501 Add test
remotes/github/feature/add_git_commands 694cc4c Add git commands description in readme
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 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.
报错了:
是因为,现在远程分支和当前分支已经不是fast-forwards关系了!
我们又不能使用git push -f直接强行推送,该怎么办呢?
\1. 变基
\2. Merge
我们先fetch一下:
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git fetch github
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:OnlyOnexl/git_learning
694cc4c..1059f72 feature/add_git_commands -> github/feature/add_git_commands
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git branch -av #可以看到这里本地分支feature/add_git_commands 超前了一个commit,又落后了一个commit!
* feature/add_git_commands 1c03316 [ahead 1, behind 1] Add git command in index.html
master b1c97fa Merge remote-tracking branch 'github/main'
suling 30c1501 Add test
temp 30c1501 Add test
remotes/github/feature/add_git_commands 1059f72 Fix deadme
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
🍀 我们团队如果不考虑一条分支情况下,这里直接使用merge方法:因为2个用户修改的是不同文件,也不会发生冲突,git是很智能的,它会自动帮我们合并的!
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git merge github/feature/add_git_commands
Merge made by the 'recursive' strategy.
readme | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
# mege后就能查看readme已经更新到最新内容了
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ cat readme
i love you,xyy!
We are going to record some git commands here.eg add and so on
# merge后,我们就可以正常推送本地仓库了
hg@LAPTOP-G8TUFE0T MINGW64 ~/Desktop/it/01-SuGit/git_learning (feature/add_git_commands)
$ git push
Counting objects: 10, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (10/10), 878 bytes | 79.00 KiB/s, done.
Total 10 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To github.com:OnlyOnexl/git_learning.git
1059f72..4e1f688 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
我们再看下版本树情况:
$ gitk --all
实验到此结束。😘
注意事项
📍 github缺省分支
现在我们在feature/add_git_commands
分支下,我们再次切换到git_learning
仓库主目录下,看下会发生什么情况?
会看到,当再一次切换到仓库主目录下,会自动切换到仓库缺省分支main
下的:
关于我
我的博客主旨:我希望每一个人拿着我的博客都可以做出实验现象,先把实验做出来,然后再结合理论知识更深层次去理解技术点,这样学习起来才有乐趣和动力。并且,我的博客内容步骤是很完整的,也分享源码和实验用到的软件,希望能和大家一起共同进步!
各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人免费帮您解决问题:
-
个人微信二维码:x2675263825 (舍得), qq:2675263825。
-
个人微信公众号:云原生架构师实战
-
个人博客地址:www.onlyonexl.cn
-
个人csdn
https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421
-
个人已开源干货😘
https://www.yuque.com/go/doc/73723298?#《不服来怼:宇宙中最好用的云笔记!& 其他开源干货》
最后
好了,关于xxx实验就到这里了,感谢大家阅读,最后贴上我女神的photo,祝大家生活快乐,每天都过的有意义哦,我们下期见!
更多推荐
所有评论(0)