当前位置: 首页 > article >正文

【Git】五、多人协作

文章目录

  • Ⅰ. 多人协作①
    • 一、准备工作
    • 二、小伙伴的操作
    • 三、本人的操作
    • 四、远程仓库分支合并操作
  • Ⅱ. 多人协作②
    • 一、两个开发者各自完成各自的任务
      • ① 小伙伴的操作
      • ② 本人的操作
    • 二、小伙伴生病了,需要我们帮它继续开发
    • 三、小伙伴恢复健康,重新由小伙伴自主开发
    • 四、合并远程仓库的两个 `feature` 分支到 `master` 分支上
      • ① 小伙伴的操作
      • ② 我们的操作
  • Ⅲ. 本地 git branch -a 依然能看到删除后的远程分支的解决办法 -- `git remote show origin` && `git remote prune origin`

在这里插入图片描述

Ⅰ. 多人协作①

一、准备工作

​ 这是我们要完成的内容:

在这里插入图片描述

​ 对于其中的开发者1,我们可以直接使用我们的云服务器,在上面进行新增;而对于开发者2,我们可以在 windows 下操作!

​ 又因为 master 分支是稳定分支,我们不是直接在 master 分支上面开发,所以要先创建一个临时分支,这里直接在码云上面创建:

在这里插入图片描述

​ 这里创建一个叫做 dev 的临时分支,然后我们回到云服务器上,尝试用 git branch -r 或者 git branch -a 指令来看看是否有 dev 分支:

[liren@VM-8-7-centos remote-test]$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
[liren@VM-8-7-centos remote-test]$

可以看到 dev 分支并不存在,为什么呢❓❓❓

​ 道理很简单,我们刚才创建是在远程仓库,而这是本地仓库,当然不受影响,所以我们现在要做的就是用 git pull 指令将远程仓库的内容拉取过来:

[liren@VM-8-7-centos remote-test]$ git pull
From gitee.com:lirendada/remote-test
 * [new branch]      dev        -> origin/dev
Already up-to-date.
[liren@VM-8-7-centos remote-test]$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
[liren@VM-8-7-centos remote-test]$

​ 拉取完就能看到 dev 分支啦!

​ 下面就是为开发者2开发环境搭建了,就是在 windows 找个地方,将仓库克隆下来即可!这里为了方便,直接在桌面创建一个目录来克隆仓库:

在这里插入图片描述

​ 到此,相当于有了两个用户,分别在 linuxwindows 上针对于同项目进行协作开发,我们的准备工作到此结束。

​ 注意:这里只是模拟了两个用户,实际开发中,每个用户都有自己的 gitee/github 账号,如果==要多人进行协同开发,必须要将用户添加进开发者,用户才有权限进行代码提交==:

在这里插入图片描述

​ 下面,我们将开发者1就作为我们自己,而开发者2也就是在 windows 在操作的叫做小伙伴!

二、小伙伴的操作

​ 小伙伴操作就是在 windows 下操作的那个用户,我们要向其拉取下来的 file.txt 进行新增 “bbb” ,但是我们还要先做处理!

​ 首先我们得先在本地仓库也创建一支临时分支,因为我们知道,本地的 master 分支也是需要稳定的,所以 一旦涉及到开发,我们就得创建临时分支,不管是本地仓库还是远程仓库都是一样

​ 这里使用的命令就是之前的指令再多加一点内容,就变成了:git checkout -b dev origin/dev

在这里插入图片描述

​ 上面还使用了 git branch -vv 指令,这个选项就是 查看本地仓库中的分支,以及其对应的远程仓库的分支

为什么要关联远程仓库的分支呢❓❓❓

​ 这是因为我们平常在拉取仓库和提交仓库的时候,都进行了简写,如 git push/pull,这样子做的前提就是本地的分支和远程仓库的分支关联起来了,如果没有关联的话,直接使用这种简写的话是会报错的,因为不知道要操作的是远程的哪只分支!

​ 这个现象我们在下面 linux 系统操作的时候可以演示一下!

​ 接着我们就在其 file.txt 文件下新增 “bbb” 文本,如下所示:

在这里插入图片描述

​ 然后将其推送到远程分支 dev 中,因为我们已经关联了,所以直接用简写的指令即可:

在这里插入图片描述

​ 最后我们去远程仓库中看看是否已经修改,记得切换为 dev 分支:

在这里插入图片描述

三、本人的操作

​ 与小伙伴的开发操作都是一样的,首先就是在本地创建临时分支,这里为了 演示不关联远程分支,所以命令为:git checkout -b dev

[liren@VM-8-7-centos remote-test]$ git checkout -b dev
Switched to a new branch 'dev'
[liren@VM-8-7-centos remote-test]$ git branch -vv
* dev    ee9229a add file
  master ee9229a [origin/master] add file

​ 可以看到此时 本地的 dev 分支并没有关联远程仓库的任何分支!也就是 还没有拉取远程仓库

​ 下面我们尝试用简化的推送命令来进行推送到远程仓库:

[liren@VM-8-7-centos remote-test]$ 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
Unpacking objects: 100% (3/3), done.
From gitee.com:lirendada/remote-test
   ee9229a..21dc5ef  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

​ 直接报了拒绝错误!这是因为我们当前的分支没有关联对应的远程仓库的哪只分支,此时要是我们直接将本地分支推送上去,并且远程仓库中两个分支也是有同一个 file.txt 文件的,它们不知道现在我们推送过去的分支是要给谁的,所以直接就拒绝了!

​ 那现在该怎么办呢❓❓❓

​ 我们可以使用错误提示中给出的 git branch --set-upstream-to=origin/<branch_name> dev 来设置本地仓库 dev 分支对应的远程仓库的分支,注意尖括号中的 branch_name 要替换成对应远程仓库的分支名:

[liren@VM-8-7-centos remote-test]$ git branch --set-upstream-to=origin/dev dev
Branch dev set up to track remote branch dev from origin.
[liren@VM-8-7-centos remote-test]$ git branch -vv
* dev    ee9229a [origin/dev: behind 1] add file
  master ee9229a [origin/master] add file

​ 下面我们向 file.txt 中新增 “aaa” 内容:

[liren@VM-8-7-centos remote-test]$ vim file.txt 
[liren@VM-8-7-centos remote-test]$ cat file.txt 
hello git
add new line;
aaa

​ 接下来将它提交并且推送到远程仓库 dev 分支中:

[liren@VM-8-7-centos remote-test]$ git add file.txt
[liren@VM-8-7-centos remote-test]$ git commit -m 'modify file.txt: aaa'
[dev 526f313] modify file.txt: aaa
 1 file changed, 2 insertions(+), 1 deletion(-)
[liren@VM-8-7-centos remote-test]$ git push origin dev
To git@gitee.com:lirendada/remote-test.git
 ! [rejected]        dev -> dev (non-fast-forward)
error: failed to push some refs to 'git@gitee.com:lirendada/remote-test.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details
[liren@VM-8-7-centos remote-test]$ 

​ 这时发现推送失败,因为你的小伙伴的最新提交和你推送的提交有冲突,解决办法也很简单,Git 已经提示我们了,先用 git pull 把最新的提交从 origin/dev 抓下来,然后,在本地进行合并,并解决冲突,再推送。操作如下:

[liren@VM-8-7-centos remote-test]$ git pull    # 先拉取,此时会冲突
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
[liren@VM-8-7-centos remote-test]$ cat file.txt 
hello git
add new line;
<<<<<<< HEAD
aaa
=======
bbb
>>>>>>> 21dc5ef00537ae0da2299d71a9b57c4a4fc60b14
[liren@VM-8-7-centos remote-test]$ vim file.txt     # 修改冲突内容
[liren@VM-8-7-centos remote-test]$ cat file.txt 
hello git
add new line;
aaa
bbb

[liren@VM-8-7-centos remote-test]$ git add file.txt     # 再次提交和推送,就成功了
[liren@VM-8-7-centos remote-test]$ git commit -m 'modify conflict'
[dev 7a26c0f] modify conflict
[liren@VM-8-7-centos remote-test]$ git push
Counting objects: 10, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 559 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:lirendada/remote-test.git
   21dc5ef..7a26c0f  dev -> dev
[liren@VM-8-7-centos remote-test]$ 

​ 此时推送完我们再次去 gitee 上看一下是否修改成功:

在这里插入图片描述

四、远程仓库分支合并操作

​ 此时可以看到远程仓库中 master 分支下的内容还未改动:

在这里插入图片描述

​ 通常有两种方案用于合并 masterdev 分支:

  • 第一种就是通过 pull request 提交申请文档给仓库管理人员,提交完之后由管理人员审核,如果通过了则由管理人员去合并,不需要我们管了!这种是 比较推荐的,特别是在多人协作的时候,一般管理人员都是老板或者项目经理,这样子做的 好处是为了保证代码的安全!但是因为这里要学习 git 指令,这里就不演示这部分的方案!
  • 第二种方案就是由我们自己在本地先合并之后推送到远程仓库!这种方案其实是 不太建议 的,因为最好还是要让别人审核你的代码,保证安全,但是因为学习需要,这里我们就采用这种方法:
    1. 首先,切换到本地的 master 分支上,进行远程仓库的拉取。因为远程分支比当前本地分支要新,需要先用 git pull 试图合并,这是一个 好习惯
    2. 为了防止有冲突,我们先切换到临时分支 dev 上合并 master 分支,如果有冲突的话则解决冲突,这也是一个 好习惯
    3. 没有冲突或者解决掉冲突后,再用 master 分支合并 dev 分支。
    4. 然后用 git push origin branch-name 指令推送到远程仓库的 master 分支!
    5. 最后删除临时分支。

​ 下面的操作,两个开发者中哪个来操作都行,我们选择我们自己这边角色来操作:

# 切换至master分⽀, pull一下,保证本地的master是最新内容。
# 合并前这么做是⼀个好习惯
[liren@VM-8-7-centos remote-test]$ git branch 
* dev
  master
[liren@VM-8-7-centos remote-test]$ git checkout master 
Switched to branch 'master'
[liren@VM-8-7-centos remote-test]$ git pull origin master
From gitee.com:lirendada/remote-test
 * branch            master     -> FETCH_HEAD
Already up-to-date.

# 切换至 dev 分支, 合并 master 分支
# 这么做是因为如果有冲突,可以在dev分支上进行处理,而不是直接在master上解决冲突。
# 这么做是⼀个好习惯
[liren@VM-8-7-centos remote-test]$ git checkout dev 
Switched to branch 'dev'
[liren@VM-8-7-centos remote-test]$ git merge master
Already up-to-date.

# 切换至master分支,合并dev分支
[liren@VM-8-7-centos remote-test]$ git checkout master 
Switched to branch 'master'
[liren@VM-8-7-centos remote-test]$ git merge dev
Updating ee9229a..7a26c0f
Fast-forward
 file.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
[liren@VM-8-7-centos remote-test]$ cat file.txt 
hello git
add new line;
aaa
bbb

# 将master分支推送到远端仓库中对应的分支上
[liren@VM-8-7-centos remote-test]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
[liren@VM-8-7-centos remote-test]$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:lirendada/remote-test.git
   ee9229a..7a26c0f  master -> master
[liren@VM-8-7-centos remote-test]$ git status
# On branch master
nothing to commit, working directory clean

​ 此时,查看远端仓库,master 分支已经是最新代码了:

在这里插入图片描述

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

在这里插入图片描述

Ⅱ. 多人协作②

​ 这次的目的和上次基本一样,只不过条件变了:

在这里插入图片描述

​ 一般情况下,如果有多需求需要多人同时进行开发,是不会在一个分支上进行多人开发,而是 一个需求或一个功能点就要创建一个 feature 分支

​ 现在同时有两个需求需要你和你的小伙伴进行开发,那么你们俩便可以各自创建一个分支来完成自己的工作。在上个部分我们已经了解了可以从码云上直接创建远程分支,其实在 本地创建的分支也可以通过推送的方式发送到远端。但是实际开发中,还是更推荐前者,这里只不过是为了学的更全面一些!下面我们就用这种本地方式来推送一下!

一、两个开发者各自完成各自的任务

① 小伙伴的操作

​ 首先我们现在 本地创建一个临时分支 feature-1,此时它是没办法关联仓库的分支的,因为我们采用的方法是在本地推送分支给远程仓库,此时远程仓库中不存在临时分支给我们关联进行开发!

在这里插入图片描述

​ 然后在本地创建一个 function1.txt,往里面新增随意内容即可:

在这里插入图片描述

​ 接着就是将新增文件提交到本地仓库中:

PS C:\Users\利刃\Desktop\git-test\remote-test> git add .
PS C:\Users\利刃\Desktop\git-test\remote-test> git commit -m "新增function1.txt"
[feature-1 22ed96d] 新增function1.txt
 1 file changed, 2 insertions(+)
 create mode 100644 function1.txt

​ 接下来就是很关键的一步,将本地的临时分支推送到远程仓库中,使用的命令是 git push origin feature-1 进行直接推送即可!

在这里插入图片描述

​ 此时在码云上就能看见我们推送上去的分支啦!

在这里插入图片描述

② 本人的操作

​ 整体的操作和上面是完全一样的:

# 首先新增feature-2本地分支
[liren@VM-8-7-centos remote-test]$ git branch -vv
* master 7a26c0f [origin/master] modify conflict
[liren@VM-8-7-centos remote-test]$ git checkout -b feature-2
Switched to a new branch 'feature-2'
[liren@VM-8-7-centos remote-test]$ git branch -vv
* feature-2 7a26c0f modify conflict
  master    7a26c0f [origin/master] modify conflict
  
 # 创建新文件
[liren@VM-8-7-centos remote-test]$ touch function2.txt
[liren@VM-8-7-centos remote-test]$ vim function2.txt 
[liren@VM-8-7-centos remote-test]$ cat function2.txt 
i am coding...

# 进行提交,并且推送到远程仓库
[liren@VM-8-7-centos remote-test]$ git add .
[liren@VM-8-7-centos remote-test]$ git commit -m '新增function2.txt'
[feature-2 bbf495f] 新增function2.txt
 1 file changed, 1 insertion(+)
 create mode 100644 function2.txt
[liren@VM-8-7-centos remote-test]$ git push origin feature-2
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 300 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'feature-2' on Gitee by visiting:
remote:     https://gitee.com/lirendada/remote-test/pull/new/lirendada:feature-2...lirendada:master
To git@gitee.com:lirendada/remote-test.git
 * [new branch]      feature-2 -> feature-2
[liren@VM-8-7-centos remote-test]$ 

​ 然后我们就能看到码云上又多了一个分支:

在这里插入图片描述

​ 并且,因为我们和小伙伴是在不同分支上操作的,所以是不会有合并冲突的情况的!但是后面我们要将两个分支合并到 master 分支的时候,就会有冲突了!

二、小伙伴生病了,需要我们帮它继续开发

​ 天有不测风云,你的小伙伴突然生病了,但需求还没开发完,需要我们帮他继续开发,于是他便把 feature-1 分支名告诉你了。这时你就需要在自己的机器上切换到 feature-1 分支帮忙继续开发!

​ 此时的操作,其实就变成了我们上面的多人协作场景一的情况,就是变成了同一个分支下开发的情况,多引入这种情况是为了熟悉操作!

​ 首先就是将远端仓库的内容拉取下来:

[liren@VM-8-7-centos remote-test]$ git branch -a
* feature-2
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/feature-2
  remotes/origin/master
  
[liren@VM-8-7-centos remote-test]$ 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
Unpacking objects: 100% (3/3), done.
From gitee.com:lirendada/remote-test
 * [new branch]      feature-1  -> origin/feature-1     # 将本地未追踪分支也拉下来了!!!
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

​ 虽然我们还没有指定当前 feature-2 分支对应的远程仓库的分支是谁,但是如果 直接使用 git pull 的话,我们发现其不仅仅拉取的是分支内的内容,还拉取了远程仓库的内容也就是其它分支

​ 所以此时我们可以看到小伙伴的分支 feature-1 也被我们拉取下来了:

[liren@VM-8-7-centos remote-test]$ git branch -a
* feature-2
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/feature-1    # 多了小伙伴的分支!!!
  remotes/origin/feature-2
  remotes/origin/master

​ 接着我们就在本地创建一个 feature-1 分支,然后将其与远程仓库中的 feature-1 关联起来:

[liren@VM-8-7-centos remote-test]$ git checkout -b feature-1 origin/feature-1
Branch feature-1 set up to track remote branch feature-1 from origin.
Switched to a new branch 'feature-1'
[liren@VM-8-7-centos remote-test]$ git branch -a
* feature-1
  feature-2
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/feature-1
  remotes/origin/feature-2
  remotes/origin/master
[liren@VM-8-7-centos remote-test]$

​ 然后我们就可以在当前分支上帮助小伙伴继续开发了:

[liren@VM-8-7-centos remote-test]$ ls
file.txt  function1.txt  README.en.md  README.md
[liren@VM-8-7-centos remote-test]$ cat function1.txt 
i am coding...
Done!!!
[liren@VM-8-7-centos remote-test]$ vim function1.txt 
[liren@VM-8-7-centos remote-test]$ cat function1.txt 
i am coding...
Done!!!
i help my friend, coding...

# 三板斧
[liren@VM-8-7-centos remote-test]$ git add .
[liren@VM-8-7-centos remote-test]$ git commit -m 'help modify function1.txt'
[feature-1 e5a80e0] help modify function1.txt
 1 file changed, 2 insertions(+), 1 deletion(-)
[liren@VM-8-7-centos remote-test]$ git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 302 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:lirendada/remote-test.git
   22ed96d..e5a80e0  feature-1 -> feature-1
[liren@VM-8-7-centos remote-test]$

​ 然后到远程仓库看一下是否变化:

在这里插入图片描述

三、小伙伴恢复健康,重新由小伙伴自主开发

​ 这时小伙伴已经修养的差不多,可以继续进行自己的开发工作,那么 他首先要获取到你帮他开发的内容,然后接着你的代码继续开发。或者你已经帮他开发完了,那他也需要在自己的电脑上看看你帮他写的代码:

在这里插入图片描述

​ 拉取无效的原因是小伙伴没有指定本地 feature-1 分支与远程 origin/feature-1 分支的关联,根据错误提示,通过设置 feature-1origin/feature-1 的链接即可:

在这里插入图片描述

​ 目前,小伙伴的本地代码和远端保持严格一致。你和你的小伙伴可以继续在不同的分支下进行协同开发了。

​ 然后小伙伴就将自己的代码开发完后再次推送到远程仓库中去:

在这里插入图片描述

​ 最后看一下码云是否有变化:

在这里插入图片描述

四、合并远程仓库的两个 feature 分支到 master 分支上

​ 各自功能开发完毕后,不要忘记我们需要将代码合并到 master 中才算真正意义上的开发完毕。

​ 此时的分支模型如下所示:

在这里插入图片描述

​ 下面我们先从小伙伴开始操作!

① 小伙伴的操作

​ 这里我们演示一下另一种我们更推荐的方式:直接在远程仓库也就是码云上提交申请表来申请合并

​ 首先我们到码云点击 pull request

在这里插入图片描述

​ 在这里面我们可以看到申请表等内容:
在这里插入图片描述

​ 提交之后,就等审核人批准,一旦批准了就会自动合并到分支上去,一般审核人是老板或者项目经理等角色,这是为了保障代码的安全!

​ 现在因为提交之后,我们自己就是审核人,所以我们来批准一下:

在这里插入图片描述

​ 通过后我们再去看看 master 分支内容是否更新:

在这里插入图片描述

② 我们的操作

​ 这里我们就演示本地操作的方式!其实就和多人协作场景一中合并操作是一样的了!

​ 此时的分支模型如下:
在这里插入图片描述

# 切换至master分支, pull一下,保证本地的master是最新内容。
# 合并前这么做是⼀个好习惯
[liren@VM-8-7-centos remote-test]$ git checkout master 
Switched to branch 'master'
[liren@VM-8-7-centos remote-test]$ git pull
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
From gitee.com:lirendada/remote-test
   7a26c0f..0d31a9e  master     -> origin/master
   e5a80e0..8f90c20  feature-1  -> origin/feature-1
Updating 7a26c0f..0d31a9e
Fast-forward
 function1.txt | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 function1.txt
 
# 切换至 feature-2 分支, 合并 master 分支
# 这么做是因为如果有冲突,可以在 feature-2 分支上进行处理,而不是在 master 上解决冲突。
# 这么做是⼀个好习惯
[liren@VM-8-7-centos remote-test]$ git checkout feature-2
Switched to branch 'feature-2'
[liren@VM-8-7-centos remote-test]$ git merge master 
Merge made by the 'recursive' strategy.
 function1.txt | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 function1.txt
[liren@VM-8-7-centos remote-test]$ ls
file.txt  function1.txt  function2.txt  README.en.md  README.md

# 1、由于 feature-2 分支已经 merge 进来了新内容,为了保证远程分支最新,所以最好 push ⼀下。
# 2、要 push 的另⼀个原因是因为在实际的开发中,master 的合并操作⼀般不是由我们自己在本地进行的,其他人或某些平台merge时,操作的肯定是远程分支,所以就要保证远程分支的最新。
# 3、如果 merge 出现冲突,不要忘记需要 commit 才可以 push!!
[liren@VM-8-7-centos remote-test]$ git status
# On branch feature-2
nothing to commit, working directory clean
[liren@VM-8-7-centos remote-test]$ git push origin feature-2
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 296 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:lirendada/remote-test.git
   bbf495f..9b28016  feature-2 -> feature-2
   
# 切换⾄ master 分支,合并 feature-2 分支
[liren@VM-8-7-centos remote-test]$ git checkout master 
Switched to branch 'master'
[liren@VM-8-7-centos remote-test]$ git merge feature-2
Updating 0d31a9e..9b28016
Fast-forward
 function2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 function2.txt
 
# 将 master 分支推送至远端
[liren@VM-8-7-centos remote-test]$ git status 
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
[liren@VM-8-7-centos remote-test]$ git push origin master 
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:lirendada/remote-test.git
   0d31a9e..9b28016  master -> master
[liren@VM-8-7-centos remote-test]$

​ 最后就能看到分支已经都合并进来了:

在这里插入图片描述

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

​ 分支模型如下所示:

在这里插入图片描述

Ⅲ. 本地 git branch -a 依然能看到删除后的远程分支的解决办法 – git remote show origin && git remote prune origin

​ 当前我们已经删除了远程的几个分支,使用 git branch -a 命令可以查看所有本地分支和远程分支,但发现很多在远程仓库已经删除的分支在本地依然可以看到:

[liren@VM-8-7-centos remote-test]$ git branch -a
  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 地址,远程分支,还有本地分支与之相对应关系等信息:

[liren@VM-8-7-centos remote-test]$ git remote show origin
* remote origin
  Fetch URL: git@gitee.com:lirendada/remote-test.git
  Push  URL: git@gitee.com:lirendada/remote-test.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':
    feature-1 merges with remote feature-1
    master    merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)
[liren@VM-8-7-centos remote-test]$

​ 此时我们可以看到那些远程仓库已经不存在的分支,根据提示,使用 git remote prune origin 命令:

[liren@VM-8-7-centos remote-test]$ git remote prune origin
Pruning origin
URL: git@gitee.com:lirendada/remote-test.git
 * [pruned] origin/dev
 * [pruned] origin/feature-1
 * [pruned] origin/feature-2
[liren@VM-8-7-centos remote-test]$ git branch -a
  feature-1
  feature-2
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
[liren@VM-8-7-centos remote-test]$ 

​ 这样就删除了那些远程仓库不存在的分支!
在这里插入图片描述


http://www.kler.cn/a/557191.html

相关文章:

  • 041集——封装之:新建图层(CAD—C#二次开发入门)
  • 新学一个JavaScript 的 classList API
  • win11系统无法打开软件_组策略无法打开_gpedit.msc不生效_为了对电脑进行保护,已经阻止此应用---Windows工作笔记057
  • C++ 项目:Unsplash 爬虫与瀑布流实战
  • Perplexity AI:通过OpenAI与DeepSeek彻底革新搜索和商业策略
  • 【C语言】第五期——函数
  • windows下docker使用笔记
  • 重构谷粒商城07:Git一小时快速起飞指南
  • 12.Docker 的资源限制
  • Github 开源 AI 知识库推荐
  • Ansible 学习笔记
  • Python----数据结构(哈希表:哈希表组成,哈希冲突)
  • Java函数式接口的巧妙应用
  • 2025.2.19——1500
  • uniapp 整合openlayers 编辑图形文件并上传到服务器
  • 从零搭建微服务项目(全)
  • QARepVGG--含demo实现
  • 蓝桥杯——lcd显示
  • 【Agent的革命之路——LangGraph】工作流中的 Command 模式
  • XiaoMi Mi5(gemini) 刷入Ubuntu Touch 16.04——安卓手机刷入Linux