【Git】-- 多人协作

文章目录

  • [5. 多人协作](#5. 多人协作)
    • [5.1 查看分支](#5.1 查看分支)
      • [5.1.1 查看远程仓库的分支](#5.1.1 查看远程仓库的分支)
      • [5.1.2 查看本地和远程的分支](#5.1.2 查看本地和远程的分支)
      • [5.1.3 查看本地和远程分支的关系](#5.1.3 查看本地和远程分支的关系)
    • [5.2 场景1(同一分支下)](#5.2 场景1(同一分支下))
      • [5.2.1 开发者1](#5.2.1 开发者1)
      • [5.2.2 开发者2](#5.2.2 开发者2)
      • [5.2.3 合并](#5.2.3 合并)
        • [5.2.3.1 本地处理](#5.2.3.1 本地处理)
        • [5.2.3.2 远程仓库处理PR(推荐)](#5.2.3.2 远程仓库处理PR(推荐))
    • [5.3 场景2(不同分支下)](#5.3 场景2(不同分支下))
      • [5.3.1 开发者1](#5.3.1 开发者1)
      • [5.3.2 开发者2](#5.3.2 开发者2)
        • [5.3.2.1 拉取主分支最新的代码](#5.3.2.1 拉取主分支最新的代码)
        • [5.3.2.2 创建并切换到分支](#5.3.2.2 创建并切换到分支)
        • [5.3.2.3 创建function2文件](#5.3.2.3 创建function2文件)
        • [5.3.2.4 推送到远程仓库](#5.3.2.4 推送到远程仓库)
      • [5.3.3 发生特殊情况](#5.3.3 发生特殊情况)
        • [5.3.3.1 开发者1帮助开发者2进行开发](#5.3.3.1 开发者1帮助开发者2进行开发)
        • [5.3.3.2 开发者2重新进入开发](#5.3.3.2 开发者2重新进入开发)
      • [5.3.4 合并](#5.3.4 合并)
        • [5.3.4.1 feature2分支合并到master(PR)](#5.3.4.1 feature2分支合并到master(PR))
        • [5.3.4.2 feature1分支合并到master(本地 + PR)](#5.3.4.2 feature1分支合并到master(本地 + PR))

更多Git相关知识:Git专栏

5. 多人协作

5.1 查看分支

5.1.1 查看远程仓库的分支

bash 复制代码
git branch -r

5.1.2 查看本地和远程的分支

bash 复制代码
git branch -a

5.1.3 查看本地和远程分支的关系

bash 复制代码
git branch -vv

5.2 场景1(同一分支下)

在master分支下:开发者1:在file.txt文件下新增"111"。开发者2:在file.txt文件下新增"222"。

条件:两个开发者在同一个分支下协作完成。

我们在进行修改的时候不能直接在master主分支下直接进行修改,需要创建一个其他的分支。

远程创建dev分支:

5.2.1 开发者1

  1. 查看分支情况
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master

本地分支:master、origin/master

远程分支:master、dev

  1. 创建本地dev分支,并与远程dev分支建立联系
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git checkout -b dev origin/dev
branch 'dev' set up to track 'origin/dev'.
Switched to a new branch 'dev'


root@VM-0-3-ubuntu:~/remote-gitcode# git branch -vv
* dev    099c367 [origin/dev] add .git ignore
  master 099c367 [origin/master] add .git ignore

本地分支:master、origin/master、dev、origin/dev

远程分支:master、dev

  1. 对文件进行修改
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# vim file.txt
 
root@VM-0-3-ubuntu:~/remote-gitcode# cat file.txt 
hello git
hello world
aaa
  1. 提交修改
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git add .

root@VM-0-3-ubuntu:~/remote-gitcode# git commit -m "modify file.txt: aaa"
[dev 31d8df8] modify file.txt: aaa
 1 file changed, 1 insertion(+)

root@VM-0-3-ubuntu:~/remote-gitcode# git push 
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 281.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag 6c4c569f
To gitee.com:pepper-cloth/remote-gitcode.git
   099c367..31d8df8  dev -> dev

5.2.2 开发者2

  1. 查看分支
powershell 复制代码
PS D:\code\remote-gitcode> git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master

本地:master、origin/master

远程:master、dev

  1. 在本地创建并切换到dev分支,不和远程仓库中的dev分支建立连接
powershell 复制代码
PS D:\code\remote-gitcode> git checkout -b dev
Switched to a new branch 'dev'


PS D:\code\remote-gitcode> git branch -vv
* dev    099c367 add .git ignore
  master 099c367 [origin/master] add .git ignore

本地:master、origin/master、dev

远程:master、dev

此时本地的dev分支和远程仓库中的dev分支并没有建立连接,如果直接使用git pull或者git push这样的省略命令,命令并不能达到预期。

powershell 复制代码
PS D:\code\remote-gitcode> git pull
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 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 261 bytes | 37.00 KiB/s, done.
From https://gitee.com/pepper-cloth/remote-gitcode
   099c367..31d8df8  dev        -> origin/dev
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> dev

本地:master、origin/master、dev、origin/dev

远程:master、dev

建立连接:

powershell 复制代码
PS D:\code\remote-gitcode> git branch --set-upstream-to=origin/dev dev
branch 'dev' set up to track 'origin/dev'.

PS D:\code\remote-gitcode> git branch -vv
* dev    099c367 [origin/dev: behind 1] add .git ignore
  master 099c367 [origin/master] add .git ignore
  1. 修改file.txt文件
powershell 复制代码
PS D:\code\remote-gitcode> cat .\file.txt
hello git
hello world
bbb
  1. 提交修改
powershell 复制代码
PS D:\code\remote-gitcode> git add .

PS D:\code\remote-gitcode> git commit -m "modify file.txt: bbb"
[dev e9adda6] modify file.txt: bbb
 1 file changed, 1 insertion(+)

PS D:\code\remote-gitcode> git push
To https://gitee.com/pepper-cloth/remote-gitcode.git
 ! [rejected]        dev -> dev (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/pepper-cloth/remote-gitcode.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

可以看到,我们在进行push的时候,被远程仓库拒绝了,提示当前的分支和远程分支有差异。原因就是因为开发者1已经修改了file.txt文件,发生冲突了。

  1. 拉取远程仓库
powershell 复制代码
PS D:\code\remote-gitcode> git pull
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.


PS D:\code\remote-gitcode> cat .\file.txt
hello git
hello world
<<<<<<< HEAD
bbb
=======
aaa
  1. 修改冲突
powershell 复制代码
PS D:\code\remote-gitcode> cat .\file.txt
hello git
hello world
aaa
bbb
  1. 提交到远程仓库
powershell 复制代码
PS D:\code\remote-gitcode> git add .

PS D:\code\remote-gitcode> git commit -m "fix"
[dev 81dfad4] fix

PS D:\code\remote-gitcode> git push
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 20 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 538 bytes | 538.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag 814f65b3
To https://gitee.com/pepper-cloth/remote-gitcode.git
   31d8df8..81dfad4  dev -> dev

5.2.3 合并

5.2.3.1 本地处理
  1. 拉取dev分支的内容
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# cat file.txt 
hello git
hello world
aaa

root@VM-0-3-ubuntu:~/remote-gitcode# git branch
* dev
  master

root@VM-0-3-ubuntu:~/remote-gitcode# git pull
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (6/6), 518 bytes | 518.00 KiB/s, done.
From gitee.com:pepper-cloth/remote-gitcode
   31d8df8..81dfad4  dev        -> origin/dev
Updating 31d8df8..81dfad4
Fast-forward
 file.txt | 1 +
 1 file changed, 1 insertion(+)
 
root@VM-0-3-ubuntu:~/remote-gitcode# cat file.txt 
hello git
hello world
aaa
bbb
  1. 切换到master主分支,并拉取master分支,需要保证master分支是最新的状态,否则后面还会出现冲突。
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

root@VM-0-3-ubuntu:~/remote-gitcode# git pull
Already up to date.
  1. 切换到dev分支,并合并master(有冲突的话直接在dev分支上解决,不影响到master主分支)。
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git checkout dev
Switched to branch 'dev'
Your branch is up to date with 'origin/dev'.

root@VM-0-3-ubuntu:~/remote-gitcode# git merge master
Already up to date.
  1. 切换到master分支,master分支merge dev分支
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

root@VM-0-3-ubuntu:~/remote-gitcode# git merge dev
Updating 099c367..81dfad4
Fast-forward
 file.txt | 2 ++
 1 file changed, 2 insertions(+)
 
root@VM-0-3-ubuntu:~/remote-gitcode# cat file.txt 
hello git
hello world
aaa
bbb
  1. 提交到远程仓库中
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git push
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag f4442ca8
To gitee.com:pepper-cloth/remote-gitcode.git
   099c367..81dfad4  master -> master
  1. 删除dev分支
5.2.3.2 远程仓库处理PR(推荐)


5.3 场景2(不同分支下)

在远程仓库下新增两个文件:开发者1新增function1,开发者2新增function2。要求在不同分支下协作完成。

5.3.1 开发者1

5.3.1.1 创建并切换分支(本地创建)
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git checkout -b feature-1
Switched to a new branch 'feature-1'
5.3.1.2新建function1文件
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# vim function1

root@VM-0-3-ubuntu:~/remote-gitcode# cat function1 
i am coding.....
Done!!!
5.3.1.3推送到远程仓库
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git push origin feature-1 
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 289 bytes | 289.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag 424b5290
remote: Create a pull request for 'feature-1' on Gitee by visiting:
remote: https://gitee.com/pepper-cloth/remote-gitcode/pull/new/pepper-cloth:feature-1...pepper-cloth:master
To gitee.com:pepper-cloth/remote-gitcode.git
 * [new branch]      feature-1 -> feature-1


root@VM-0-3-ubuntu:~/remote-gitcode# git branch -a
* feature-1
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-1
  remotes/origin/master

5.3.2 开发者2

5.3.2.1 拉取主分支最新的代码
powershell 复制代码
PS D:\code\remote-gitcode> git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  
PS D:\code\remote-gitcode> git pull
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 269 bytes | 44.00 KiB/s, done.
From https://gitee.com/pepper-cloth/remote-gitcode
   099c367..81dfad4  master     -> origin/master
 * [new branch]      feature-1  -> origin/feature-1
Updating 099c367..81dfad4
Fast-forward
 file.txt | 2 ++
 1 file changed, 2 insertions(+)
5.3.2.2 创建并切换到分支
powershell 复制代码
PS D:\code\remote-gitcode> git checkout -b feature-2
Switched to a new branch 'feature-2'
5.3.2.3 创建function2文件
powershell 复制代码
PS D:\code\remote-gitcode> cat .\function2.txt
i am coding......
Done!!!
5.3.2.4 推送到远程仓库
powershell 复制代码
PS D:\code\remote-gitcode> git add .

PS D:\code\remote-gitcode> git commit -m "add function2"
[feature-2 86e23f7] add function2
 1 file changed, 2 insertions(+)
 create mode 100644 function2.txt

PS D:\code\remote-gitcode> git push origin feature-2
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 20 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 288 bytes | 288.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag 8c45f5b7
remote: Create a pull request for 'feature-2' on Gitee by visiting:
remote: https://gitee.com/pepper-cloth/remote-gitcode/pull/new/pepper-cloth:feature-2...pepper-cloth:master
To https://gitee.com/pepper-cloth/remote-gitcode.git
 * [new branch]      feature-2 -> feature-2

PS D:\code\remote-gitcode> git branch -a
* feature-2
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-1
  remotes/origin/feature-2
  remotes/origin/master

5.3.3 发生特殊情况

假设现在开发者2小伙伴,家里有点事需要请假,拜托开发者1帮他继续进行开发。开发者1开发了一部分之后,开发者2回来继续工作了。

5.3.3.1 开发者1帮助开发者2进行开发

拉取分支:

bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git branch -a
  feature-1
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-1
  remotes/origin/master

root@VM-0-3-ubuntu:~/remote-gitcode# git pull
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 268 bytes | 268.00 KiB/s, done.
From gitee.com:pepper-cloth/remote-gitcode
 * [new branch]      feature-2  -> origin/feature-2
Already up to date.
root@VM-0-3-ubuntu:~/remote-gitcode# git branch -a
  feature-1
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-1
  remotes/origin/feature-2
  remotes/origin/master

创建本地分支,并与远程的对应分支建立链接:

bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git checkout -b feature-
2 origin/feature-2
branch 'feature-2' set up to track 'origin/feature-2'.
Switched to a new branch 'feature-2'

root@VM-0-3-ubuntu:~/remote-gitcode# git branch -a
  feature-1
* feature-2
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-1
  remotes/origin/feature-2
  remotes/origin/master

开发者1开发:

bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# cat function2.txt
i am coding......
Done!!!

root@VM-0-3-ubuntu:~/remote-gitcode# vim function2.txt 

root@VM-0-3-ubuntu:~/remote-gitcode# cat function2.txt 
i am coding......
Done!!!
i am coding.....
Done!

推送到远端:

bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git add .

root@VM-0-3-ubuntu:~/remote-gitcode# git commit -m "modify function2"
[feature-2 576c8c8] modify function2
 1 file changed, 3 insertions(+), 1 deletion(-)
 
root@VM-0-3-ubuntu:~/remote-gitcode# git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag a4400588
To gitee.com:pepper-cloth/remote-gitcode.git
   86e23f7..576c8c8  feature-2 -> feature-2
5.3.3.2 开发者2重新进入开发
  1. 拉取feature-2分支下的内容

在feature-2分支上进行pull的话,并不会达到我们的预期。因为开发者2的本地feature-2分支和远程仓库中的对应分支没有建立连接。

powershell 复制代码
PS D:\code\remote-gitcode> git pull
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 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 264 bytes | 37.00 KiB/s, done.
From https://gitee.com/pepper-cloth/remote-gitcode
   86e23f7..576c8c8  feature-2  -> origin/feature-2
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> feature-2

建立连接并拉取:

powershell 复制代码
PS D:\code\remote-gitcode> git branch --set-upstream-to=origin/feature-2 feature-2
branch 'feature-2' set up to track 'origin/feature-2'.
PS D:\code\remote-gitcode> git pull
Updating 86e23f7..576c8c8
Fast-forward
 function2.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
  1. 开发
powershell 复制代码
PS D:\code\remote-gitcode> cat .\function2.txt
i am coding......
Done!!!
i am coding.....
Done!
i am coding.....
Done!
  1. 推送到远端
powershell 复制代码
PS D:\code\remote-gitcode> git add .

PS D:\code\remote-gitcode> git commit -m "modify function2"
[feature-2 22068af] modify function2
 1 file changed, 3 insertions(+)
 
PS D:\code\remote-gitcode> git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 20 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 278 bytes | 278.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag 99465c9b
To https://gitee.com/pepper-cloth/remote-gitcode.git
   576c8c8..22068af  feature-2 -> feature-2

5.3.4 合并

5.3.4.1 feature2分支合并到master(PR)




5.3.4.2 feature1分支合并到master(本地 + PR)
  1. 拉取最新的master
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
root@VM-0-3-ubuntu:~/remote-gitcode# git pull
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (4/4), 1.12 KiB | 1.13 MiB/s, done.
From gitee.com:pepper-cloth/remote-gitcode
   81dfad4..df695c5  master     -> origin/master
   576c8c8..22068af  feature-2  -> origin/feature-2
Updating 81dfad4..df695c5
Fast-forward
 function2.txt | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 function2.txt
  1. 切换到feature-1分支上,并合并master
bash 复制代码
root@VM-0-3-ubuntu:~/remote-gitcode# git checkout feature-1
Switched to branch 'feature-1'

root@VM-0-3-ubuntu:~/remote-gitcode# git merge master
Merge made by the 'ort' strategy.
 function2.txt | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 function2.txt

root@VM-0-3-ubuntu:~/remote-gitcode# git push origin feature-1
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 309 bytes | 309.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.23]
remote: Set trace flag 644d35b5
To gitee.com:pepper-cloth/remote-gitcode.git
   e93b608..82a6c67  feature-1 -> feature-1

执行merge命令会进入这个页面,ctrl + X退出即可。

  1. 申请合并



  2. 删除feature-1和feature-2分支
相关推荐
roamingcode3 小时前
从混沌到秩序:Git Diff 结构化报告的 Claude Code Skill 实践
人工智能·git·agent·skill·claude code·领域知识包·ai经验复用
175063319453 小时前
EtherCAT ubuntu wireshark
网络·ubuntu·wireshark
ss2734 小时前
idea中git更新项目:将传入更改合并到当前分支,在传入更改上变基当前分支
java·git·intellij-idea
hopsky4 小时前
好用!Git 同时开发多个分支
git
weixin_462446234 小时前
ubuntu真机安装tljh jupyterhub支持跨域iframe
linux·运维·ubuntu
小小ken4 小时前
ubuntu通过vmware workstation安装win10虚拟机简要步骤及排错
ubuntu·vmware·虚拟机
小小ken5 小时前
ubuntu添加新网卡时,无法自动获取IP原因及解决办法
linux·网络·tcp/ip·ubuntu·dhcp
secondyoung6 小时前
Git使用:rebase用法
c语言·经验分享·git·vscode
LuckyLay7 小时前
Ubuntu配置多版本Java,自由切换
java·linux·ubuntu