
🔥个人主页:Cx330🌸
❄️个人专栏:《C语言》《LeetCode刷题集》《数据结构-初阶》《C++知识分享》
《优选算法指南-必刷经典100题》《Linux操作系统》:从入门到入魔
🌟心向往之行必能至
🎥Cx330🌸的简介:

前言:
在如今的软件开发场景中,"单打独斗" 早已成为过去式。无论是初创团队的快速迭代,还是大型企业的跨部门协作,多人同时参与同一个项目是常态。而 Git 作为分布式版本控制系统的标杆,凭借其强大的分支管理、版本回溯和协同能力,成为了团队协作的 "基础设施"。但很多开发者在单人使用 Git 时得心应手,一到多人协作就会遇到代码冲突、分支混乱、版本不一致等问题,甚至出现 "删库跑路" 的惊险场景。本文将从实际工作场景出发,拆解 Git 多人协作的核心流程、实用技巧和避坑指南,帮助团队高效协同,让代码管理更规范、更省心
目录
[一. 多人协作模式一:同一分支协同开发(简单场景)](#一. 多人协作模式一:同一分支协同开发(简单场景))
[二. 协作模式二:多分支并行开发(推荐场景)](#二. 协作模式二:多分支并行开发(推荐场景))
[三. 远程分支删除后,本地 git branch -a 依然能看到的解决办法](#三. 远程分支删除后,本地 git branch -a 依然能看到的解决办法)
[结尾:让 Git 成为团队协作的 "润滑剂"](#结尾:让 Git 成为团队协作的 “润滑剂”)
一. 多人协作模式一:同一分支协同开发(简单场景)
适用于小团队、短期迭代,多人基于同一dev分支或同一feature分支开发(需高频同步,减少冲突)。

核心流程 :"拉取→开发→提交→推送" 闭环,注意拉取这一步不要少
目前,我们所完成的工作如下:
- 基本完成 Git 的所有本地库的相关操作,git基本操作,分支理解,版本回退,冲突解决等等。
- 申请码云账号,将远端信息clone到本地,以及推送和拉取
是时候干最重要的一件事情了,实现多人协作开发!为了做这些事情,我们需要先做一些准备工作
我们在 windows 环境下,再 clone 同一个项目仓库,来模拟和我一起协同开发的伙伴:
在新目录下"右键",点击在终端中打开:


此时,就能发现我们clone成功了,在目录下也能看到我们远程仓库

我们在码云上创建一个dev分支:

创建成功:

创建成功的远程分支是可以通过 Git 拉取到本地来,以实现完成本地开发工作。
接下来让我们和另一名开发的小伙伴都将远程仓库进行一次拉取操作,并观察结果;
- 对于我们要操作的是:
bash
yhr@VM-24-15-ubuntu:~/gitcode$ git pull
From gitee.com:ynua-haoran/gitcode
* [new branch] dev -> origin/dev
Already up to date.
yhr@VM-24-15-ubuntu:~/gitcode$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/master
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout -b dev origin/dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
Switched to a new branch 'dev'
拉取后便可以看到远程的 dev 分支,接着切换到 dev 分支供我们进行本地开发。要说明的是,我们切换到的是本地的 dev 分支,根据示例中的操作,会将本地分支和远程分支进行关系链接
- 对于小伙伴要操作的是:

我们想要拉取时,失败了,这是因为我们没有与远程构建链接,我们只需要使用最后一行代码即可:

git branch -vv:查看远程连接情况
此时我们小伙伴那要在 file.txt 文件中新增 bbb ,我们打印出来看一下:

现在,我们小伙伴完成了开发,将要上传到 master分支上:

我们push失败了,在之前Git远程仓库同步代码时,我们就讲过,同一个文件,不同内容push时会冲突,这时候就需要我们修改文件:

再来拉取一下:

此时我们再来push:

这时候我们就已经推送成功了,我们在远程看一下 file.txt 文件内容:

此时仓库情况:

这时候master分支是没有 aaa bbb 这两行代码的,此时就需要我们合并到master分支:

我们先不在这里这样做,我们在本地上进行合并操作:

最后不要忘记,虽然我们是分支上进行多人协作开发,但最终的目的是要将开发后的代码合并到master上去,让我们项目运行最新的代码,接下来我们就需要进行这件事情了:
bash
yhr@VM-24-15-ubuntu:~/gitcode$ git branch
* dev
master
yhr@VM-24-15-ubuntu:~/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), 530 bytes | 530.00 KiB/s, done.
From gitee.com:ynua-haoran/gitcode
4d488cc..5ec7b0d dev -> origin/dev
Updating 4d488cc..5ec7b0d
Fast-forward
file.txt | 3 +++
1 file changed, 3 insertions(+)
yhr@VM-24-15-ubuntu:~/gitcode$ cat file.txt
hello git
hello world
aaa
bbb
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
yhr@VM-24-15-ubuntu:~/gitcode$ git pull
Already up to date.
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout dev
Switched to branch 'dev'
Your branch is up to date with 'origin/dev'.
yhr@VM-24-15-ubuntu:~/gitcode$ git merge master
Already up to date.
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
yhr@VM-24-15-ubuntu:~/gitcode$ git merge dev
Updating 602b6fc..5ec7b0d
Fast-forward
file.txt | 4 ++++
1 file changed, 4 insertions(+)
yhr@VM-24-15-ubuntu:~/gitcode$ cat file.txt
hello git
hello world
aaa
bbb
yhr@VM-24-15-ubuntu:~/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 c2436993
To gitee.com:ynua-haoran/gitcode.git
602b6fc..5ec7b0d master -> master
我们在远程仓库查看一下 file.txt 文件:

此时,dev分支对于我们来说就没用了,那么dev分支就可以被删除掉,我们可以直接在远程仓库中将dev分支删除掉:

总结一下,在同一分支下进行多人协作的工作模式通常是这样:
- 首先,可以试图用 git push origin branch-name 推送自己的修改;
- 如果推送失败,则因为远程分支比你的本地的更新,需要先用 git pull(这里建议可以每次推送前都用用,好习惯)
- 如果合并有冲突,则解决冲突,并在本地提交;
- 没有冲突或者解决冲突后,再用 git push origin branch-name 推送就能成功!
- 功能开发完毕,将分支 merge 进 master,最后删除分支
二. 协作模式二:多分支并行开发(推荐场景)
企业级协作主流模式,每人基于dev创建独立feature分支,开发完成后合并到dev,避免相互干扰

完整流程 :从功能开发到合并上线
一般情况下,如果有多需求需要多人同时进行开发,是不会在一个分支上进行多人开发,而是需要一个需求或一个功能点就要创建一个 feature分支。
现在同时有两个需求需要你和你的小伙伴进行开发,那么你们俩便可以各自创建一个分支来完成自己的工作。在上过部分我们已经了解了可以从码云上直接创建远程分支,其实在本地创建的分支来可以通过推送的方式发送到远端,在这个部分我们就来用一下这种方式
- 对于你来说,可以进行以下操作:
bash
yhr@VM-24-15-ubuntu:~/gitcode$ git branch -a
dev
* master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/master
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout -b feature-1
Switched to a new branch 'feature-1'
yhr@VM-24-15-ubuntu:~/gitcode$ vim function1
yhr@VM-24-15-ubuntu:~/gitcode$ git add .
yhr@VM-24-15-ubuntu:~/gitcode$ git commit -m "add fun1"
[feature-1 d837dff] add fun1
1 file changed, 2 insertions(+)
create mode 100644 function1
yhr@VM-24-15-ubuntu:~/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), 280 bytes | 280.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 8f4653b6
remote: Create a pull request for 'feature-1' on Gitee by visiting:
remote: https://gitee.com/ynua-haoran/gitcode/pull/new/ynua-haoran:feature-1...ynua-haoran:master
To gitee.com:ynua-haoran/gitcode.git
* [new branch] feature-1 -> feature-1
yhr@VM-24-15-ubuntu:~/gitcode$ git branch -a
dev
* feature-1
master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/feature-1
remotes/origin/master
- 对于小伙伴来说,可以进行以下操作:

在目录下创建function2.txt文件,在文件里新加一行代码,保存退出,然后推送至远程仓库:

此时我们去码云上查看:

对于你的 feature-1 分支:

对于小伙伴的 feature-2 分支:

正常情况下,你们俩就可以在自己的分支上进行专业的开发了!
但天有不测风云,你的小伙伴突然生病了,但需求还没开发完,需要你帮他继续开发,于是他便把 feature-2 分支名告诉你了。这时你就需要在自己的机器上切换到 feature-2 分支帮忙继续开发,要做的操作如下:
bash
yhr@VM-24-15-ubuntu:~/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), 265 bytes | 265.00 KiB/s, done.
From gitee.com:ynua-haoran/gitcode
* [new branch] 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-1
yhr@VM-24-15-ubuntu:~/gitcode$ git branch -a
dev
* feature-1
master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/feature-1
remotes/origin/feature-2
remotes/origin/master
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout -b feature-2 origin/feature-2
Branch 'feature-2' set up to track remote branch 'feature-2' from 'origin'.
Switched to a new branch 'feature-2'
yhr@VM-24-15-ubuntu:~/gitcode$ vim function2.txt
yhr@VM-24-15-ubuntu:~/gitcode$ git add .
yhr@VM-24-15-ubuntu:~/gitcode$ git commit -m "md fun2"
[feature-2 e30d165] md fun2
1 file changed, 2 insertions(+), 1 deletion(-)
yhr@VM-24-15-ubuntu:~/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), 263 bytes | 263.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 2b451995
To gitee.com:ynua-haoran/gitcode.git
af95c24..e30d165 feature-2 -> feature-2
查看远程状态,推送成功了:

这时,你的小伙伴已经修养的差不多,可以继续进行自己的开发工作,那么他首先要获取到你帮他开发的内容,然后接着你的代码继续开发,我们将function2.txt文件新增 Done!作为开完完成的标志,进行推送:

Pull 无效的原因是小伙伴没有指定本地 feature-2 分支与远程 origin/feature-2 分支的链接,根据提示,设置 feature-2 和 origin/feature-2 的链接即可
这里博主刚刚将 PowerShell 退出去了,所以这里大家可能会出错,提醒一下大家
目前,小伙伴的本地代码和远端保持严格一致。你和你的小伙伴可以继续在不同的分支下进行协同开发了。
各自功能开发完毕后,不要忘记我们需要将代码合并到master中才算真正意义上的开发完毕。
由于你的小伙伴率先开发完毕,于是开始merge :
bash
yhr@VM-24-15-ubuntu:~/gitcode$ git branch -a
dev
* feature-1
feature-2
master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/feature-1
remotes/origin/feature-2
remotes/origin/master
# 切换至 master分子,pull 一下,保证本地的 master是最新内容
# 合并前这么做是一个好习惯
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
yhr@VM-24-15-ubuntu:~/gitcode$ git pull
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (1/1), 937 bytes | 937.00 KiB/s, done.
From gitee.com:ynua-haoran/gitcode
5ec7b0d..263bdff master -> origin/master
Updating 5ec7b0d..263bdff
Fast-forward
function2.txt | 3 +++
1 file changed, 3 insertions(+)
create mode 100644 function2.txt
# 切换至 feature-1 分支,合并 master 分支
# 这么做是因为如果有冲突,可以在 feature-1 分支上进行处理,而不是在在master上解决冲突
yhr@VM-24-15-ubuntu:~/gitcode$ git checkout feature-1
Switched to branch 'feature-1'
yhr@VM-24-15-ubuntu:~/gitcode$ git merge master
Merge made by the 'ort' strategy.
function2.txt | 3 +++
1 file changed, 3 insertions(+)
create mode 100644 function2.txt
# 1. 由于feature-1分支已经merge进来了新内容,为了保证远程分支最新,所以最好push一下。
# 2. 要push的另一个原因是因为在实际的开发中,master的merge操作一般不是由我们自己在本地进行操作,其他人员或某些平台merge时,操作的肯定是远程分支,所以就要保持远程分支的最新。
# 3. 如果 merge 出现冲突,不要忘记需要commit才可以push!!
yhr@VM-24-15-ubuntu:~/gitcode$ git status
On branch feature-1
nothing to commit, working tree clean
yhr@VM-24-15-ubuntu:~/gitcode$ git branch -vv
dev 5ec7b0d [origin/dev] merge dev
* feature-1 4482b18 Merge branch 'master' into feature-1
feature-2 e30d165 [origin/feature-2: behind 1] md fun2
master 263bdff [origin/master] !1 feature-2合并到master Merge pull request !1 from 原浩然/feature-2
yhr@VM-24-15-ubuntu:~/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), 304 bytes | 304.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 f94c8bb2
To gitee.com:ynua-haoran/gitcode.git
d837dff..4482b18 feature-1 -> feature-1
此时远程仓库的状态:

此时,feature-1 和 feature-2 分支对于我们来说就没用了,那么我们可以直接在远程仓库中将dev分支删除掉:
三. 远程分支删除后,本地 git branch -a 依然能看到的解决办法
当前我们已经删除了远程的几个分支,使用 git branch -a 命令可以查看所有本地分支和远程分支,但发现很多在远程仓库已经删除的分支依然可以看到。例如:
bash
yhr@VM-24-15-ubuntu:~/gitcode$ git branch -a
dev
feature-1
feature-2
* master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/feature-1
remotes/origin/feature-2
remotes/origin/master
使用命令 git remote show origin ,可以查看 remote 地址,远程分子,还有本地分子与之相对应关系等信息。
bash
yhr@VM-24-15-ubuntu:~/gitcode$ git remote show origin
* remote origin
Fetch URL: git@gitee.com:ynua-haoran/gitcode.git
Push URL: git@gitee.com:ynua-haoran/gitcode.git
HEAD branch: master
Remote branches:
master tracked
refs/remotes/origin/dev stale (use 'git remote prune' to remove)
refs/remotes/origin/feature-1 stale (use 'git remote prune' to remove)
refs/remotes/origin/feature-2 stale (use 'git remote prune' to remove)
Local branches configured for 'git pull':
dev merges with remote dev
feature-2 merges with remote feature-2
master merges with remote master
Local ref configured for 'git push':
master pushes to master (local out of date)
此时我们可以看到那些远程仓库已经不存在的分支,根据提示,使用 git remote prune origin命令:
bash
yhr@VM-24-15-ubuntu:~/gitcode$ git remote prune origin
Pruning origin
URL: git@gitee.com:ynua-haoran/gitcode.git
* [pruned] origin/dev
* [pruned] origin/feature-1
* [pruned] origin/feature-2
yhr@VM-24-15-ubuntu:~/gitcode$ git branch -a
dev
feature-1
feature-2
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
这样就删除了那些·远程仓库不存在的分支。对于本地仓库的删除,之前的课程已经学过了,大家可以自行操作:**git branch -d 【分支名】**即可完成删除
结尾:让 Git 成为团队协作的 "润滑剂"
Git 多人协作的核心不是死记硬背命令,而是建立 "规范流程 + 协作意识"。一个团队只要统一了分支模型、提交规范和协作流程,就能最大程度减少冲突和误操作,让每个开发者都能专注于代码本身。当然,Git 的能力远不止于此,后续还可以探索 CI/CD 与 Git 的结合、多人协作中的权限管理等进阶内容。希望本文能帮助你和团队打通 Git 协作的 "任督二脉",让协作更高效、开发更顺畅。如果你有其他协作场景中的问题或技巧,欢迎在评论区交流分享!