Git多人协作

目录

[1. 多人协作一](#1. 多人协作一)

[2. 多人协作二](#2. 多人协作二)

[3. 远程分支删除后,本地git brahch -a依旧能看到的解决方法](#3. 远程分支删除后,本地git brahch -a依旧能看到的解决方法)


1. 多人协作一

目前,我们所完成的工作如下:

  • 基本完成Git的所有本地库的相关操作,git基本操作,分支理解,版本回退,冲突解决等等

  • 申请码云账号,将远端信息clone到本地,以及推送和拉取。

是时候干最重要的一件事情了,实现多人协作开发!为了做这件事情,我们需要先做一些准备工作。我们之前已经将项目clone到了指定目录,如;

bash 复制代码
aurora@wanghao:~/remote-gitcode$ pwd
/home/aurora/remote-gitcode

我们在windows环境下,再clone同一个项目仓库,来模拟和你一起协作开发的另一名小伙伴:

clone成功。

注意,我们这里是模拟了两个用户,实际开发中,每个用户都有自己的gitee/github账号,如果要多人进

行协同开发,必须要将用户添加进开发者,用户才有权限进行代码提交:

邀请用户:

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

目前,我们的仓库中只有一个master主分支,但在实际的项目开发中,在任何情况下其实都是不允许直接在master分支上修改代码的,这是为了保证主分支的稳定。所以在开发新功能时,常常会新建其他分支,供开发时进行迭代使用。

那么接下来,就让我们在gitee上新建dev远程分支供我们使用:

创建成功:

创建成功的远程分支是可以通过Git拉取到本地来,以实现完成本地开发工作。

我们可以使用两个命令来查看本地分支和远端分支有哪些?

bash 复制代码
aurora@wanghao:~/remote-gitcode$ git branch 
* master
aurora@wanghao:~/remote-gitcode$ git branch -r
  origin/HEAD -> origin/master
  origin/master

对于我们本地来讲,有一个本地master分支,其实还能看到远程的master分支。我们所有的操作是在本地的分支上进行操作的,这个时候不管是本地的dev分支还是远端的dev分支,我们都看不到,这个时候我们可以进行pull操作。

接下来让我们和另一名开发的小伙伴都将远程仓库进行一次拉取操作,并观察结果:

对于我们要操作的是:

bash 复制代码
aurora@wanghao:~/remote-gitcode$ git pull #我们直接可以使用git pull拉去相关内容
From gitee.com:Axurea/remote-gitcode
 * [new branch]      dev        -> origin/dev # dev分支对应远端的dev分支
Already up to date.
aurora@wanghao:~/remote-gitcode$ git branch -a # -a代表既打印本地又打印远程的
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master

之前说的pull操作和push操作都是对于分支的操作,我们push必须要让两个分支之间建立连接,有了这个连接,才可以push,对于push操作,我们给它指定origin,指定了远程,也指定了推送的是哪个分支,这个时候其实是不需要建立连接的,那什么时候用到建立的连接呢?就是我们想要简写(git push)的时候,其他的分支也是一样的。本地master和远端master克隆的时候就自动建立连接,所以pull的时候会生效。

拉取后便可以看到远程的dev分支,接着切换到dev分支供我们进行本地开发。要说明的是,我们切换到的是本地的dev分支,根据示例中的操作,会将本地分支和远程分支的进行关系链接。

对于小伙伴要操作的是:

我们在 dev 分支上开发:

bash 复制代码
# 创建本地分支dev并和远程分支dev建立连接
aurora@wanghao:~/remote-gitcode$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
aurora@wanghao:~/remote-gitcode$ git checkout -b dev origin/dev 
Branch 'dev' set up to track remote branch 'dev' from 'origin'. # dev分支已经追踪了远程的dev分支,其实就是将本地dev分支和远程dev分支建立连接
Switched to a new branch 'dev'
aurora@wanghao:~/remote-gitcode$ git branch -a
* dev
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
  
# git branch -vv 选项查看连接
aurora@wanghao:~/remote-gitcode$ git branch -vv
* dev    f777118 [origin/dev] md
  master f777118 [origin/master] md
  
# 对本地file.txt进行修改
aurora@wanghao:~/remote-gitcode$ vim file.txt 
aurora@wanghao:~/remote-gitcode$ git add .
aurora@wanghao:~/remote-gitcode$ git commit -m "md file.txt: aaa"
[dev 33980a7] md file.txt: aaa
 1 file changed, 1 insertion(+)
aurora@wanghao:~/remote-gitcode$ git status
On branch dev
Your branch is ahead of 'origin/dev' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
aurora@wanghao:~/remote-gitcode$ git push # 本地dev和远端dev建立好了连接,可以直接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), 273 bytes | 273.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag a44f2483
To gitee.com:Axurea/remote-gitcode.git
   f777118..33980a7  dev -> dev

我们来看一下远端:

我们的小伙伴在 dev 分支上开发:

bash 复制代码
PS E:\git\remote-gitcode> git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
PS E:\git\remote-gitcode> git checkout -b dev # 没有和远端dev建立连接
Switched to a new branch 'dev'
PS E:\git\remote-gitcode> git branch -vv
* dev    f777118 md
  master f777118 [origin/master] md
  
# 没有和远端建立连接,那么我们使用git pull试一下 -- 拉取失败
PS E:\git\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), done.
From https://gitee.com/Axurea/remote-gitcode
   f777118..33980a7  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
    
# 使用命令来建立连接
PS E:\git\remote-gitcode> git branch --set-upstream-to=origin/dev dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'. #分支"dev"已设置为从"origin"跟踪远程分支"dev"。
# 我们再次查看一下
PS E:\git\remote-gitcode> git  branch -vv
* dev    f777118 [origin/dev: behind 1] md
  master f777118 [origin/master] md

# 在file.txt上开发
PS E:\git\remote-gitcode> cat .\file.txt
hello git
hello world
PS E:\git\remote-gitcode> cat .\file.txt
hello git
hello world
bbb

# add commit push -- 冲突了
PS E:\git\remote-gitcode> git add .
PS E:\git\remote-gitcode> git commit -m "md file.txt: bbb"
[dev ea08551] md file.txt: bbb
 1 file changed, 1 insertion(+)
PS E:\git\remote-gitcode> git push
To https://gitee.com/Axurea/remote-gitcode.git
 ! [rejected]        dev -> dev (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/Axurea/remote-gitcode.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

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

bash 复制代码
PS E:\git\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 E:\git\remote-gitcode> cat .\file.txt
hello git
hello world
<<<<<<< HEAD
bbb
=======
aaa
>>>>>>> 33980a775221ac0272013e15b0ecf57bf7efa041

解决冲突,并重新推送:

bash 复制代码
PS E:\git\remote-gitcode> git status
On branch dev
Your branch and 'origin/dev' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")
PS E:\git\remote-gitcode> git add .
PS E:\git\remote-gitcode> git commit -m "merge dev"
[dev 2ed9b3d] merge dev
PS E:\git\remote-gitcode> git push
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 6 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 540 bytes | 540.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag c9458187
To https://gitee.com/Axurea/remote-gitcode.git
   33980a7..2ed9b3d  dev -> dev

此时,我们看到远端的码云已经能看到我们的新提交了!

由此,两名开发者已经开始可以进行协同开发了,不断的gitpull/addcommit/push,遇到了

冲突,就使用我们之前讲的冲突处理解决掉冲突。

对于你来说,要想看到小伙伴的代码,只需要pul1一下即可,例如:

bash 复制代码
aurora@wanghao:~/remote-gitcode$ cat file.txt 
hello git
hello world
aaa
aurora@wanghao:~/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), 520 bytes | 520.00 KiB/s, done.
From gitee.com:Axurea/remote-gitcode
   33980a7..2ed9b3d  dev        -> origin/dev
Updating 33980a7..2ed9b3d
Fast-forward
 file.txt | 1 +
 1 file changed, 1 insertion(+)
aurora@wanghao:~/remote-gitcode$ cat file.txt 
hello git
hello world
aaa
bbb

最后不要忘记,虽然我们是在分支上进行多人协作开发,但最终的目的是要将开发后的代码合并到master上去,让我们的项目运行最新的代码。接下来我们就需要做这件事情了:

1、PR->申请单(推荐)

我们提交的这个申请单时需要经过审查员的审核的,往往是项目经历或老板,是有一个保障性的,我们再进行merge操作就非常安全了。

新建Pull Requests。

源分支选择dev分支,目标分支选择master分支。

下面就自动为我们跳出来了申请单,Pull Requests内容就是当时我们新建仓库的时候选择了一个Pull Requests模板文件,我们申请者只有填写了这些申请的内容之后对于我们的审查人员才可以看到为什么要将我们源分支去进行合并,合并到我们的目标分支中,申请单就是这么一个作用,我们也要标题写一些。

2、本地

bash 复制代码
# 保证dev分支下是最新的
aurora@wanghao:~/remote-gitcode$ git pull
Already up to date.
aurora@wanghao:~/remote-gitcode$ cat file.txt 
hello git
hello world
aaa
bbb

# 切换至master分支,pull一下,保证本地的master是最新内容
# 合并前这么做是一个好习惯
aurora@wanghao:~/remote-gitcode$ git checkout master 
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
aurora@wanghao:~/remote-gitcode$ git pull
Already up to date.

# 切换至dev分支,合并master分支
# 这里做是因为如果有冲突,可以再dev分支上进行处理,而不是再master上解决冲突
# 这么做是一个好习惯
aurora@wanghao:~/remote-gitcode$ git checkout dev
Switched to branch 'dev'
Your branch is up to date with 'origin/dev'.
aurora@wanghao:~/remote-gitcode$ git merge master 
Already up to date.

# 切换至master分支,合并dev分支
aurora@wanghao:~/remote-gitcode$ git checkout master 
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
aurora@wanghao:~/remote-gitcode$ git merge dev
Updating f777118..2ed9b3d
Fast-forward
 file.txt | 2 ++
 1 file changed, 2 insertions(+)
aurora@wanghao:~/remote-gitcode$ cat file.txt 
hello git
hello world
aaa
bbb

# master分支推送到远端
aurora@wanghao:~/remote-gitcode$ 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 tree clean
aurora@wanghao:~/remote-gitcode$ git push
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 344c0ba8
To gitee.com:Axurea/remote-gitcode.git
   f777118..2ed9b3d  master -> master
aurora@wanghao:~/remote-gitcode$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

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

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

总结一下,在同一分支下进行多人协作的工作模式通常是这样:

  • 首先,可以试图用gitpushoriginbranch-name推送自己的修改;

  • 如果推送失败,则因为远程分支比你的本地更新,需要先用gitpull试图合并;

  • 如果合并有冲突,则解决冲突,并在本地提交;解决冲突是非常麻烦的。

  • 没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!

  • 功能开发完毕,将分支merge进master,最后删除分支。

2. 多人协作二

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

现在同时有两个需求需要你和你的小伙伴进行开发,那么你们俩便可以各自创建一个分支来完成自己的工作。在上个部分我们已经了解了可以从码云上直接创建远程分支,其实在本地创建的分支也可以通过推送的方式发送到远端。在这个部分我们就来用一下这种方式。

多人协作一那块是远程创建分支,那么这里呢我们使用本地创建分支,然后push到远端。对于创建本地分支和创元远程分支,这两种都可以,推荐的话推荐创建远程分支。远程创建分支基于master分支创建的话我可以理解为远程的master分支是最新最全的代码,本地的master不能保证是最新的代码,保证是最新的要使用pull操作的。

对于开发者1,可以进行以下操作:

bash 复制代码
# 新增本地分支feature-1并切换
aurora@wanghao:~/remote-gitcode$ git checkout -b feature-1 # 我们没有远程分支,所以没办法建立连接
Switched to a new branch 'feature-1'
aurora@wanghao:~/remote-gitcode$ git branch 
  dev
* feature-1
  master

#新增内容需求-创建function1文件
aurora@wanghao:~/remote-gitcode$ vim function1
aurora@wanghao:~/remote-gitcode$ cat function1 
i am conding...
Done!

# feature-1分支推送到远端
aurora@wanghao:~/remote-gitcode$ git add .
aurora@wanghao:~/remote-gitcode$ git commit -m "add func1"
[feature-1 e8cbe22] add func1
 1 file changed, 2 insertions(+)
 create mode 100644 function1
aurora@wanghao:~/remote-gitcode$ git push # push是不行的,因为远程和本地没有建立连接
fatal: The current branch feature-1 has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature-1
aurora@wanghao:~/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), 286 bytes | 286.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 874f4f9f
remote: Create a pull request for 'feature-1' on Gitee by visiting:
remote: https://gitee.com/Axurea/remote-gitcode/pull/new/Axurea:feature-1...Axurea:master
To gitee.com:Axurea/remote-gitcode.git
 * [new branch]      feature-1 -> feature-1
# 查看
aurora@wanghao:~/remote-gitcode$ git branch -a
  dev
* feature-1
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/feature-1
  remotes/origin/master

我们来看一下远端:

我们的码云上也确实有了这个分支。

对于开发者2,可以进行以下操作:

bash 复制代码
# 查看所处的分支
PS E:\git\remote-gitcode> git branch -a
* dev
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master

# 切换到master分支
PS E:\git\remote-gitcode> git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

# pull,保证master分支为最新的代码
PS E:\git\remote-gitcode> cat .\file.txt
hello git
hello world
PS E:\git\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), done.
From https://gitee.com/Axurea/remote-gitcode
   f777118..2ed9b3d  master     -> origin/master
 * [new branch]      feature-1  -> origin/feature-1
Updating f777118..2ed9b3d
Fast-forward
 file.txt | 2 ++
 1 file changed, 2 insertions(+)
PS E:\git\remote-gitcode> cat .\file.txt
hello git
hello world
aaa
bbb

# 新增本地分支feature-2并切换
PS E:\git\remote-gitcode> git checkout -b feature-2
Switched to a new branch 'feature-2'

#新增内容需求-创建function2文件
PS E:\git\remote-gitcode> cat .\function2.txt
i am coding...

# feature-2分支推送到远端
PS E:\git\remote-gitcode> git add .
PS E:\git\remote-gitcode> git commit -m "add fun2"
[feature-2 647d844] add fun2
 1 file changed, 1 insertion(+)
 create mode 100644 function2.txt
PS E:\git\remote-gitcode> git push origin feature-2
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 6 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 282 bytes | 282.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 494f8e8c
remote: Create a pull request for 'feature-2' on Gitee by visiting:
remote: https://gitee.com/Axurea/remote-gitcode/pull/new/Axurea:feature-2...Axurea:master
To https://gitee.com/Axurea/remote-gitcode.git
 * [new branch]      feature-2 -> feature-2

我们来看一下远端:

正常情况下,你俩就可以在自己的分支上进行专业的开发了!这样是非常方便,push就成功,没有冲突的问题。将来我们在公司中进行代码开发的时候,基本上都是一个功能对应着一个分支。

但夫有不测风云,你的小伙伴突然生病了,但需求还没开发完,需要你帮他继续开发,于是他便把feature-2分支名告诉你了。这时你就需要在自己的机器上切换到feature-2分支帮忙继续开发,要做的操作如下:

bash 复制代码
# 必须先拉去远端仓库内容
aurora@wanghao:~/remote-gitcode$ git branch -a
  dev
* feature-1
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/feature-1
  remotes/origin/master
aurora@wanghao:~/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), 262 bytes | 262.00 KiB/s, done.
From gitee.com:Axurea/remote-gitcode
 * [new branch]      feature-2  -> origin/feature-2
There is no tracking information for the current branch.
.........

# 可以看到远程已经有了feature-2
aurora@wanghao:~/remote-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
  
  # 切换到feature-2分支上,可以和远程feature-2分支关联起来
  # 否则将来只使用 git push推送内容会失败
  aurora@wanghao:~/remote-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'
aurora@wanghao:~/remote-gitcode$ ls
a.so  b.so  c.so  d.so  file.txt  function2.txt  LICENSE  README.en.md  README.md

没有建立连接,为什么还可以使用短的git pull命令呢?对于git pull命令来说有两种情况:

  1. 拉取分支内的内容

  2. 拉取仓库的内容

切换成功后,便可以看见feature-2分支中的function2文件了,接着就可以帮小伙伴进行开发:

bash 复制代码
# 继续开发
aurora@wanghao:~/remote-gitcode$ vim function2.txt 
aurora@wanghao:~/remote-gitcode$ cat function2.txt 
i am coding...
i am coding...

# 推送内容
aurora@wanghao:~/remote-gitcode$ git add .
aurora@wanghao:~/remote-gitcode$ git commit -m "md func2"
[feature-2 9ce71f8] md func2
 1 file changed, 2 insertions(+), 1 deletion(-)
aurora@wanghao:~/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), 265 bytes | 265.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 464a6195
To gitee.com:Axurea/remote-gitcode.git
   647d844..9ce71f8  feature-2 -> feature-2

查看远程状态,推送成功了:

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

bash 复制代码
PS E:\git\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), done.
From https://gitee.com/Axurea/remote-gitcode
   647d844..9ce71f8  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

PS E:\git\remote-gitcode> cat .\function2.txt
i am coding...

Pull无效的原因是小伙伴没有指定本地feature-2分支与远程origin/feature-2分支的链接,根据提示,设置feature-2和origin/feature-2的链接即可:

bash 复制代码
PS E:\git\remote-gitcode> git branch --set-upstream-to=origin/feature-2 feature-2
Branch 'feature-2' set up to track remote branch 'feature-2' from 'origin'.
PS E:\git\remote-gitcode> git pull
Updating 647d844..9ce71f8
Fast-forward
 function2.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
PS E:\git\remote-gitcode> cat .\function2.txt
i am coding...
i am coding...

# 继续开发
PS E:\git\remote-gitcode> cat .\function2.txt
i am coding...
i am coding...
Done!

# 提交
PS E:\git\remote-gitcode> git add .
PS E:\git\remote-gitcode> git commit -m "md func2"
[feature-2 93125cc] md func2
 1 file changed, 1 insertion(+)
PS E:\git\remote-gitcode> git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 6 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 271 bytes | 271.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 984ae589
To https://gitee.com/Axurea/remote-gitcode.git
   9ce71f8..93125cc  feature-2 -> feature-2

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

远程仓库刷新,确实也看到我们写的内容了。

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

将feature-1分支和feature-1分支合并进master分支,这次合并我们可以选择发起Pull Requests申请单的方式来完成合并。

新建Pull Requests。

让feature-2分支合并到master分支,源分支就选feature-2,目标分支就选master分支。

所有的填写完成之后我们就点击创建Pull Requests。

创建完成之后Pull Requests上面就有个1了。

到这个页面就不是开发人员来操作了,这个是审查人员和测试人员来做的。

往下翻,点击问年,这个文件里面就给我们演示了我们的源分支和目标分支的差异,对于目标分支是新增了一个文件

审查人员看完代码没有问题的话,右上角点已阅。

已阅完成之后就可以点击右上角的代码评审,如果通过的话就点通过,审查通过,测试通过即可,

接下来就合并我们的分支。点击接受Pull Requests。

接受完了之后我们可以看到审查和测试的工作其实都做完了,对于我们的Pull Requests已经显示已合并的状态。

切换到master,我们其实就可以看到有一个function2的文件。

接下来我们将feature-1合并到master,像上面的操作其实也是可以的,如果两个分支没有在同一个文件夹下修改的话是没有冲突的。

也就是我们看到的这种,允许我们可自动合并的情况,它是没有冲突的,我们直接创建Pull Requests是可以的。

但是就是害怕feature-1分支和master分支最新的提交有冲突,那此时我们又回到了之前说的,要解决冲突。先让feature-1合并master分支,前面提到的,好的建议。有冲突的话我们在自己的本地feature-1分支上去解决冲突,不影响master。

bash 复制代码
# 切换至master分支,pull以一下,保证本地的master是最新的内容
# 合并前这么做是一个好习惯
aurora@wanghao:~/remote-gitcode$ git checkout master 
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
aurora@wanghao:~/remote-gitcode$ git pull
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (4/4), 1.14 KiB | 1.14 MiB/s, done.
From gitee.com:Axurea/remote-gitcode
   2ed9b3d..569974a  master     -> origin/master
   9ce71f8..93125cc  feature-2  -> origin/feature-2
Updating 2ed9b3d..569974a
Fast-forward
 function2.txt | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 function2.txt


# 切换至 feature-1分支,合并master分支
# 这里做是因为如果有冲突,可以在feature-1分支上进行处理,而不是在master上解决冲突。
# 这么做是一个好习惯
aurora@wanghao:~/remote-gitcode$ git checkout feature-1 
Switched to branch 'feature-1'

# 我们查看状态,发现没有让我们push的操作,这是因为本地的feature-1没有对应的远程分支,此时我们push就要用长的push命令
aurora@wanghao:~/remote-gitcode$ git status 
On branch feature-1
nothing to commit, working tree clean
aurora@wanghao:~/remote-gitcode$ git barnch -vv
git: 'barnch' is not a git command. See 'git --help'.

The most similar command is
        branch
aurora@wanghao:~/remote-gitcode$ git branch -vv
  dev       2ed9b3d [origin/dev] merge dev
* feature-1 9f5eefd Merge branch 'master' into feature-1
  feature-2 9ce71f8 [origin/feature-2: behind 1] md func2
  master    569974a [origin/master] !2 feature-2合并到master Merge pull request !2 from Aurora/feature-2

# 1、由于feature-1分支已经merge进来了新内容,为了保证远程分支最新,所以最好push一下。
# 2、要push的另一个原因是因为在实际的开发中,master的merge操作一般不是由我们自己在本地操作的,
# 其他人员或某些平台merge时,操作的肯定是远程分支,所以要保证远程分支的最新。
# 3、如果merge出现冲突,不要忘记需要commit才能push!!
aurora@wanghao:~/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), 307 bytes | 307.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 1a4227bb
To gitee.com:Axurea/remote-gitcode.git
   e8cbe22..9f5eefd  feature-1 -> feature-1

# 切换至master分支,合并feature-1分支

# 将master分支推送至远端

merge的时候出现这个界面就说明merge的时候没有出现任何冲突。

此时我们就可以创建Pull Requests来操作了。

流程跟之前是一样的。

对于master就有了function1这个文件了。

这个时候feature-1和feature-2两个分支没用了,我们就可以删掉了。

3. 远程分支删除后,本地git brahch -a依旧能看到的解决方法

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

bash 复制代码
aurora@wanghao:~/remote-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 复制代码
aurora@wanghao:~/remote-gitcode$ git remote show origin 
* remote origin
  Fetch URL: [email protected]:Axurea/remote-gitcode.git
  Push  URL: [email protected]:Axurea/remote-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 复制代码
aurora@wanghao:~/remote-gitcode$ git remote prune origin 
Pruning origin
URL: [email protected]:Axurea/remote-gitcode.git
 * [pruned] origin/dev
 * [pruned] origin/feature-1
 * [pruned] origin/feature-2

# 再次查看
aurora@wanghao:~/remote-gitcode$ git branch -a
  dev
* feature-1
  feature-2
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

# 删除本地分支
aurora@wanghao:~/remote-gitcode$ git branch -d dev 
Deleted branch dev (was 2ed9b3d).
aurora@wanghao:~/remote-gitcode$ git branch -a
* feature-1
  feature-2
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
相关推荐
Cynthia-石头3 小时前
Git Github Gitee GitLab
git·gitee·github
ak啊17 小时前
Git 撤销操作完全指南:从工作区到远程仓库的救赎之路
git
小_路19 小时前
git 常用命令
git
Dontla21 小时前
git引用概念(git reference,git ref)(简化对复杂SHA-1哈希值的管理)(分支引用、标签引用、HEAD引用、远程引用、特殊引用)
git·算法·哈希算法
枫叶落雨2221 天前
Git 使用规范指南
git
德育处主任2 天前
聚沙成塔,三步成书:GitBook极简入门教程
前端·git·电子书
m0_635647482 天前
git管理github上的repository
git·github
Jiude2 天前
基于开源项目开发公司项目,如何同步开源仓库的最新更新(以vben为例)
git·开源·github
PAK向日葵2 天前
【软件工程】如何使用Git Rebase同步团队开发进度
git·面试
枫叶落雨2222 天前
git checkout C1解释
git