【Git原理与使用】-- 多人协作

这篇具有很好参考价值的文章主要介绍了【Git原理与使用】-- 多人协作。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

多人协作一(多人同一分支)

开发者一(Linux)

开发者二(Windous)

master合并

远端上的合并

本地上的合并

总结

多人协作一(多人多分支)

开发者一(Linux)

开发者二(Windous)

master合并

合并function-2

合并function-1

远程分支删除后,本地 git branch -a 依然能看到的解决办法


        此处 windows 环境下,再 clone 同⼀个项目仓库,来模拟协作开发的另⼀名开发人

员。我们在 windows 环境下,执行开辟一个文件夹。

使用Shift + 鼠标右键

git 多人协作,Git,git

 此处为了简单就可以采取 HTTPS 方式进行克隆,与 Linux 一摸一样的方式即可。

git 多人协作,Git,git

        之前有所提到 master 分支是一个稳定的分支,作为开发者不要在 master 分支上做直接的修改。所以在这里我们需要创建其他的分支,这个其他分支是在本地创建,还是远程创建是都可以的。

多人协作一(多人同一分支)

目标:

        远程 master 分支下的 file.txt 文件新增代码 "aaaaaa" 、"bbbbbb"  。

实现:

        由一个开发人员添加一行 "aaaaaa" ,另一名开发人员添加一行 "bbbbbb" 。

条件:

        在一个分支下协作完成。

        此处采用远程创建dev分支。

git 多人协作,Git,git

git 多人协作,Git,git

        创建一个名为 dev 的分支。

git 多人协作,Git,git

git 多人协作,Git,git

        此时我们需要明确的知道,远程仓库就有了两个分支:master 与 dev 分支。对应的一位开发者有一个根据远程仓库克隆出来的本地仓库,而此时本地仓库中有一个本地的 master 分支。其实现在本地还有一个分支,对应的远程的 master 分支:origin/master 分支。

开发者一(Linux)

        可以使用 git branch -r 命令进行查看远程分支, git branch 命令是用于查看的是本地仓库的分支。

[qcr@ecs-205826 remote---project]$ git branch -r
  origin/HEAD -> origin/master
  origin/master

        远程是有一个 HEAD 指针的,其是与我们本地仓库所讲的 HEAD 指针作用是一样的。创建成功的远程分支是可以通过 Git 拉取到本地来,以实现完成本地开发工作。

[qcr@ecs-205826 remote---project]$ git pull
From gitee.com:chuanru/remote---project
 * [new branch]      dev        -> origin/dev
Already up-to-date.
[qcr@ecs-205826 remote---project]$ git branch -r
  origin/HEAD -> origin/master
  origin/dev
  origin/master

补充:
        命令 git branch -a 是既打印远程的也打印本地的。

[qcr@ecs-205826 remote---project]$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master

#:直接使用 git pull 就可以拉下来的原因。 

        之前有所提到,不管是 pull 操作,还是 push 操作,都是针对于分支的操作。之前我们针对于远程仓库的 master 分支,与本地仓库的 master 分支使用 push 操作:

        想要 push ,就必须让两个分支之间建立连接。采取格式为:

git push <远程主机名> <本地分⽀名>:<远程分⽀名>
 
# 如果本地分⽀名与远程分⽀名相同,则可以省略冒号:
git push <远程主机名> <本地分⽀名>

        这个时候其实是不需要建立连接的,而是想要简写的时候才需要建立连接,有了这个连接 Git 才会知道是哪个分支到哪个分支。所以可以理解为:连接的意义,指明 "方向" 。而 pull 与 push 同理。

        所以在这里,直接使用了一个 git pull 操作,而并没有去指定后面的内容,而远程仓库的 master 与本地仓库的 master 是克隆的时候自动建立连接。所以可以不用指明,直接对于双方的 master 生效。

        此时本地没有的 dev 分支,所以我们需要创建一个 dev 分支。

[qcr@ecs-205826 remote---project]$ git checkout -b dev origin/dev
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'
[qcr@ecs-205826 remote---project]$ git branch -a
* dev
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
        要说明的是,我们切换到的是本地的 dev 分支,根据示例中的操作,会将本地分支和远程分⽀的进行关系链接。利用 git branch -vv 命令可以查看本地分支与远程分支的连接请款。
[qcr@ecs-205826 remote---project]$ git branch -vv
* dev    e532e80 [origin/dev] 创建.gitignore
  master e532e80 [origin/master] 创建.gitignore

        此时对 file.txt 文件就可以进行操作了。

[qcr@ecs-205826 remote---project]$ vim file.txt 
[qcr@ecs-205826 remote---project]$ cat file.txt 
hello Git
hello world
aaaaaa

        然后便可以进行提交,push 到远程

[qcr@ecs-205826 remote---project]$ git add .
[qcr@ecs-205826 remote---project]$ git commit -m "修改文件: 新增一行aaaaaa"
[dev 0303c49] 修改文件: 新增一行aaaaaa
 1 file changed, 2 insertions(+), 1 deletion(-)
[qcr@ecs-205826 remote---project]$ git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 309 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:chuanru/remote---project.git
   e532e80..0303c49  dev -> dev

git 多人协作,Git,git

开发者二(Windous)

        此处采用本地以普通的方式创建 dev 分支,并不直接表明本地分支和远程分支的连接情况。

PS C:\Git\remote---project> git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
PS C:\Git\remote---project> git checkout -b dev
Switched to a new branch 'dev'
PS C:\Git\remote---project> git branch -vv
* dev    e532e80 创建.gitignore
  master e532e80 [origin/master] 创建.gitignore

        此时发现我们本地仓库创建的 dev 分支,确实没有任何与远程的连接。此时就可以验证我们之前所讲的连接是指明 "方向" 。也就是对于 pull 与 push 无需准确的表明方向,有连接不用表明,无连接需表明。

PS C:\Git\remote---project> git pull
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
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

        此处提示:当前分支 dev 没有连接的分支,要推送当前分支并将远程设置为连接。此处使用: git branch --set-upstream-to=origin/<branch> dev

PS C:\Git\remote---project> git branch -vv
* dev    e532e80 [origin/dev: behind 1] 创建.gitignore
  master e532e80 [origin/master] 创建.gitignore

        此时我们发现,开发者二的仓库中只有。

PS C:\Git\remote---project> cat file.txt
hello Git
hello world

        是由于开发者二的 dev 分支是来源于 master 分支的,所以没有文件的变化也是合乎常理的,于是开发者二开始他的工作。

PS C:\Git\remote---project> git branch
* dev
  master
PS C:\Git\remote---project> cat file.txt
hello Git
hello world
bbbbbb

        然后开发者二开始进行提交和 push 操作。

PS C:\Git\remote---project> git add .
PS C:\Git\remote---project> git commit -m "修改文件:新增一行bbbbbb"
[dev 073c54c] 修改文件:新增一行bbbbbb
 1 file changed, 2 insertions(+), 1 deletion(-)
PS C:\Git\remote---project> git push
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
To https://gitee.com/chuanru/remote---project.git
 ! [rejected]        dev -> dev (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/chuanru/remote---project.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 已经提示我们,先用 git pull 把最新的提交从 origin/dev 抓下来,然后,在本地进行合并,并解决冲突,再推送。
PS C:\Git\remote---project> git pull
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
PS C:\Git\remote---project> cat file.txt
hello Git
hello world
<<<<<<< HEAD
bbbbbb
=======
aaaaaa
>>>>>>> 0303c49e6d41a9afd09d8b901e8603fa02c2b36a
        解决冲突,重新推送。
PS C:\Git\remote---project> cat file.txt
hello Git
hello world
aaaaaa
bbbbbb
PS C:\Git\remote---project> git add .
PS C:\Git\remote---project> git commit -m "merge dev"
[dev 8be002b] merge dev
PS C:\Git\remote---project> git push
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 16 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 578 bytes | 578.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/chuanru/remote---project.git
   0303c49..8be002b  dev -> dev

git 多人协作,Git,git

        由此,两名开发者已经开始可以进行协同开发了,不断的 git pull/add/commit/push ,遇到了冲突,就使用我们之前讲的冲突处理解决掉冲突。并且对于我们来说,要想看到同事的代码,只需要 pull 一下即可。

master合并

        最后不要忘记,虽然我们是在分支上进行多人协作开发,但最终的目的是要将开发后的代码合并到 master  上去,让我们的项目运行最新的代码。

git 多人协作,Git,git

        在 【Git原理与使用】-- 远程操作 一文中已有所讲解到,对于master分支合并其他分支有两种方法:
  1. 本地上的合并:通过将本地上的 master 合并 dev 分支,然后通过 push 更新远端的 master 分支。
  2. 远端上的合并:通过提一个 PR(Pull Request) 。合并的申请单,在这个申请单里面要说明,为什么要进行合并,给到仓库管理员看。一旦管理人员同意了,就可以自动的执行 merge 操作。远程的 dev 分支合并到远程的 master 分支上。

远端上的合并

        PR(Pull Request)。

git 多人协作,Git,git

git 多人协作,Git,git

本地上的合并

        对于本地上的合并,情况是多样化的,以最复杂的情况为目标。

git 多人协作,Git,git

开发人员一

        此时开发者一的本地dev为。

[qcr@ecs-205826 remote---project]$ git branch
* dev
  master
[qcr@ecs-205826 remote---project]$ cat file.txt 
hello Git
hello world
aaaaaa

        切换至 dev 分支,pull ⼀下,保证本地的 dev 是最新内容(将相关的"aaaaaa" "bbbbbb"都拉下来)

[qcr@ecs-205826 remote---project]$ 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
Unpacking objects: 100% (6/6), done.
From gitee.com:chuanru/remote---project
   0303c49..8be002b  dev        -> origin/dev
Updating 0303c49..8be002b
Fast-forward
 file.txt | 1 +
 1 file changed, 1 insertion(+)
[qcr@ecs-205826 remote---project]$ cat file.txt 
hello Git
hello world
aaaaaa
bbbbbb

        接下来就为了保证 master 的最新性,合并前这么做是⼀个好习惯。

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

        切换至 master 分支,合并 dev 分支

[qcr@ecs-205826 remote---project]$ git checkout master
Switched to branch 'master'
[qcr@ecs-205826 remote---project]$ git merge dev
Updating e532e80..8be002b
Fast-forward
 file.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
[qcr@ecs-205826 remote---project]$ cat file.txt 
hello Git
hello world
aaaaaa
bbbbbb

        随后进行 push 操作即可。

[qcr@ecs-205826 remote---project]$ git push
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:chuanru/remote---project.git
   e532e80..8be002b  master -> master

git 多人协作,Git,git

        于是此时 dev 的任务完成了,dev 就没有任何用处了,也就可以删除了。

git 多人协作,Git,git

总结

在同⼀分支下进行多人协作的工作模式通常是这样:
  • 首先,可以试图用 git push origin branch-name 推送自己的修改。
  • 如果推送失败,则因为远程分支比我们的本地更新,需要先用 git pull 试图合并。
  • 如果合并有冲突,则解决冲突,并在本地提交。
  • 没有冲突或者解决掉冲突后,再用 git push origin branch-name 推送就能成功。
  • 功能开发完毕,将分支 merge 进 master,最后删除分支。

所以:同一分支下多人协作的过程下是有一些问题的。多人在同一个分支下进行开发,基本上是必然有冲突的,而解决冲突是非常麻烦的事情 —— 其实这个工作模式是不常见的

多人协作一(多人多分支)

目标:

        远程 master 分支下的新增 function1文件、function2文件。

实现:

        由一个开发人员新增 function1文件,另一名开发人员新增 function2文件。

条件:

        在不同分支下协作完成,各自让某一个功能私有一个分支。

        此处采用本地创建分支,然后再将本地分支推向远程仓库中。 

融汇贯通的理解:
git 多人协作,Git,git

        推荐选择第一个,创建远程分支,因为远程分支如基于master的新建,是能够保证远程的master分支是最新、最全、最稳定的代码,所以远程基于master创建出来的分支,也是最新、最全、最稳定的代码。

        如果通过本地的master创建的分支,由于本地的master分支不能保证是最新、最全、最稳定的代码。是需要使用pull操作来保证的,比起远端是需要更多的、更麻烦的步骤的。

开发者一(Linux)

[qcr@ecs-205826 remote---project]$ git branch -a
  dev
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
[qcr@ecs-205826 remote---project]$ git checkout -b function-1
Switched to a new branch 'function-1'

        这一次后面是没有办法跟上远程的分支的,因为对于远程来说,都没有 function-1 分支,于是便没有办法让远程分支和本地分支去建立连接。

[qcr@ecs-205826 remote---project]$ vim function1
[qcr@ecs-205826 remote---project]$ cat function1 
I am coding ……
Done
[qcr@ecs-205826 remote---project]$ git add .
[qcr@ecs-205826 remote---project]$ git commit -m "新增一个function1文件"
[function-1 740ad8d] 新增一个function1文件
 1 file changed, 2 insertions(+)
 create mode 100644 function1

        之后就该进行 push 操作了,就可以将本地分支 push 到远程仓库中。

[qcr@ecs-205826 remote---project]$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Everything up-to-date

        直接进行 push 是不可以的,因为本地并未和远程建立连接。是还有一种: git push origin 分支 的方式,直接将本地分支推送向远端。

[qcr@ecs-205826 remote---project]$ git push origin function-1 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 317 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 'function-1' on Gitee by visiting:
remote:     https://gitee.com/chuanru/remote---project/pull/new/chuanru:function-1...chuanru:master
To git@gitee.com:chuanru/remote---project.git
 * [new branch]      function-1 -> function-1

git 多人协作,Git,git

git 多人协作,Git,git

开发者二(Windous)

        首先需要保证本地仓库master分支的代码为最新、最全、最稳定的。

PS C:\Git\remote---project> git branch
  dev
* master
PS C:\Git\remote---project> cat .\file.txt
hello Git
hello world
PS C:\Git\remote---project> git pull
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
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), 297 bytes | 49.00 KiB/s, done.
From https://gitee.com/chuanru/remote---project
   e532e80..8be002b  master     -> origin/master
 * [new branch]      function-1 -> origin/function-1
Updating e532e80..8be002b
Fast-forward
 file.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
PS C:\Git\remote---project> cat .\file.txt
hello Git
hello world
aaaaaa
bbbbbb

        随后就可以创建一个最新、最全、最稳定的本地分支。

PS C:\Git\remote---project> git checkout -b function-2
Switched to a new branch 'function2'

git 多人协作,Git,git

PS C:\Git\remote---project> git add .
PS C:\Git\remote---project> git commit -m "新增文件function2"
[function-2 4c84244] 新增文件function2
 1 file changed, 2 insertions(+)
 create mode 100644 function2.txt
PS C:\Git\remote---project> git push origin function-2
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 310 bytes | 310.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'function-2' on Gitee by visiting:
remote:     https://gitee.com/chuanru/remote---project/pull/new/chuanru:function-2...chuanru:master
To https://gitee.com/chuanru/remote---project.git
 * [new branch]      function-2 -> function-2

git 多人协作,Git,git

        可以发现执行到现在,根本就不用解决任何的冲突,原因就是分支是各自私有一份的,它们是独立的。正常情况下,二者就可以在自己的分支上进行专业的开发了。

        但天有不测风云,你的同事突然生病了,但需求还没开发完,需要我们帮他继续开发,于是他便把 function-2 分支名告诉我们了。这时我们就需要在自己的机器上切换到 function-2 分支帮忙继续开发。对于开发者一就需要切换到 function-2 分支上的,这样其实又演变成了,多名开发者在一个分支上协作开发的场景。
         必须先拉取远端仓库内。
[qcr@ecs-205826 remote---project]$ 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:chuanru/remote---project
 * [new branch]      function-2 -> origin/function-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> function-1

融汇贯通的理解:
对于 pull 操作:

  1. 用于拉取分支中的内容 - 是必须需要建立连接的。
  2. 用于拉取远程仓库中的内容 - 是可以不用让分支建立连接的,因为和分支没有关系,拉取的是仓库。
        可以看到远程已经有了 function-2 分支。
[qcr@ecs-205826 remote---project]$ git branch -a
  dev
* function-1
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/function-1
  remotes/origin/function-2
  remotes/origin/master
        切换到 function-2 分支上,可以和远程的 function-2 分支关联起来,否则将来只使用 git push 推送内容会失败。
[qcr@ecs-205826 remote---project]$ git checkout -b function-2 origin/function-2
Branch function-2 set up to track remote branch function-2 from origin.
Switched to a new branch 'function-2'
        继续开发。
[qcr@ecs-205826 remote---project]$ vim function2.txt 
[qcr@ecs-205826 remote---project]$ cat function2.txt 
I am coding……
aaaaaaaaaaaaaaaaaa
Done 
        推送内容。
[qcr@ecs-205826 remote---project]$ git add .
[qcr@ecs-205826 remote---project]$ git commit -m "修改文件function2"
[function-2 f2a379e] 修改文件function2
 1 file changed, 2 insertions(+), 1 deletion(-)
[qcr@ecs-205826 remote---project]$ git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 296 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:chuanru/remote---project.git
   4c84244..f2a379e  function-2 -> function-2

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

git 多人协作,Git,git

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

PS C:\Git\remote---project> cat .\function2.txt
I am coding鈥︹€?
Done
PS C:\Git\remote---project> git pull
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
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), 276 bytes | 34.00 KiB/s, done.
From https://gitee.com/chuanru/remote---project
   4c84244..f2a379e  function-2 -> origin/function-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> function-2

PS C:\Git\remote---project> cat .\function2.txt
I am coding鈥︹€?
Done
        pull 无效的原因是同事没有指定本地 function-2 分支与远程 origin/fenction-2 分支的链接,根据提示,设置 function-2 和 origin/function-2 的链接即可。
PS C:\Git\remote---project> git branch --set-upstream-to=origin/function-2 function-2
Branch 'function-2' set up to track remote branch 'function-2' from 'origin'.
PS C:\Git\remote---project> git pull
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
Updating 4c84244..f2a379e
Fast-forward
 function2.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
PS C:\Git\remote---project> cat .\function2.txt
I am coding鈥︹€?
aaaaaaaaaaaaaaaaaa
Done
        目前,同事的本地代码和远端保持严格⼀致。我们和同事就可以继续在不同的分支下进行协同开发了。

master合并

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

git 多人协作,Git,git

合并function-2

        切换至 master ,进行 pull 保证本地 master 是最新内容。

PS C:\Git\remote---project> git branch
  function-2
* master
PS C:\Git\remote---project> git pull
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
Already up to date.

       切换至 function-2 分支,合并 master 分支。

PS C:\Git\remote---project> git checkout function-2
Switched to branch 'function-2'
Your branch is up to date with 'origin/function-2'.
PS C:\Git\remote---project> git merge master
Already up to date.

       切换至 master 分支,合并 function-2 分支。

PS C:\Git\remote---project> git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
PS C:\Git\remote---project> git merge function-2
Updating 8be002b..f2a379e
Fast-forward
 function2.txt | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 function2.txt

        将 master 分支推送至远端。

PS C:\Git\remote---project> 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 tree clean
PS C:\Git\remote---project> git push
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
info: detecting host provider for 'https://gitee.com/'...
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/chuanru/remote---project.git
   8be002b..f2a379e  master -> master
        此时远程仓库的状态。

git 多人协作,Git,git

合并function-1

        切换至 master 分支, pull 一 下,保证本地的  master  是最新内容,合并前这么做是⼀个好习惯。
[qcr@ecs-205826 remote---project]$ git branch
  function-1
  function-2
* master
[qcr@ecs-205826 remote---project]$ git pull
From gitee.com:chuanru/remote---project
   8be002b..f2a379e  master     -> origin/master
Updating 8be002b..f2a379e
Fast-forward
 function2.txt | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 function2.txt
        切换至 function-1 分支, 合并 master 分支,这么做是因为如果有冲突,可以在 function-1 分支上进行处理,而不是在  master  上解决冲突,这么做是⼀个好习惯。
[qcr@ecs-205826 remote---project]$ git function-1
git: 'function-1' is not a git command. See 'git --help'.
[qcr@ecs-205826 remote---project]$ git checkout function-1
Switched to branch 'function-1'
[qcr@ecs-205826 remote---project]$ git merge master
Merge made by the 'recursive' strategy.
 function2.txt | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 function2.txt
[qcr@ecs-205826 remote---project]$ ls
a.so  b.so  file.txt  function1  function2.txt  README.en.md  README.md
  1. 由于 function-1 分支已经 merge 进来了新内容,为了保证远程分支最新,所以最好 push 一下。
  2. 要 push 的另⼀个原因是因为在实际的开发中,master 的 merge 操作⼀般不是由我们自己在本地进其他人员或某些平台 merge 时,操作的肯定是远程分支,所以就要保证远程分支的最新。
  3. 如果 merge 出现冲突,不要忘记需要 commit 才可以 push 。
[qcr@ecs-205826 remote---project]$ git push
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 310 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:chuanru/remote---project.git
   740ad8d..e75c59c  function-1 -> function-1
        切换至 master 分支,合并function-1 分支。
[qcr@ecs-205826 remote---project]$ git checkout master
Switched to branch 'master'
[qcr@ecs-205826 remote---project]$ git merge function-1
Updating f2a379e..e75c59c
Fast-forward
 function1 | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 function1
[qcr@ecs-205826 remote---project]$ ls
a.so  b.so  file.txt  function1  function2.txt  README.en.md  README.md
        将 master 分支 推送至远端。
[qcr@ecs-205826 remote---project]$ 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
[qcr@ecs-205826 remote---project]$ git push
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:chuanru/remote---project.git
   f2a379e..e75c59c  master -> master
[qcr@ecs-205826 remote---project]$ git status
# On branch master
nothing to commit, working directory clean

        此时远程仓库的状态。

git 多人协作,Git,git

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

git 多人协作,Git,git

        这就是多人协作的工作模式,⼀旦熟悉了,就非常简单。

远程分支删除后,本地 git branch -a 依然能看到的解决办法

        当前我们已经删除了远程的几个分支,使用 git branch -a 命令可以查看所有本地分支和远程分支,但发现很多在远程仓库已经删除的分支在本地依然可以看到。之前的一系列操作后。文章来源地址https://www.toymoban.com/news/detail-713973.html

[qcr@ecs-205826 remote---project]$ git branch -a
  function-1
  function-2
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/function-1
  remotes/origin/function-2
  remotes/origin/master
        使用命令 git remote show origin ,可以查看 remote 地址,远程分支,还有本地分支与之相对应关系等信息。
[qcr@ecs-205826 remote---project]$ git branch show origin
Branch show set up to track remote branch master from origin.
[qcr@ecs-205826 remote---project]$ git remote show origin
* remote origin
  Fetch URL: git@gitee.com:chuanru/remote---project.git
  Push  URL: git@gitee.com:chuanru/remote---project.git
  HEAD branch: master
  Remote branches:
    master                         tracked
    refs/remotes/origin/dev        stale (use 'git remote prune' to remove)
    refs/remotes/origin/function-1 stale (use 'git remote prune' to remove)
    refs/remotes/origin/function-2 stale (use 'git remote prune' to remove)
  Local branches configured for 'git pull':
    function-2 merges with remote function-2
    master     merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)
        此时我们可以看到那些远程仓库已经不存在的分支,根据提示,使用  git remote prune  origin 命令。
[qcr@ecs-205826 remote---project]$ git remote prune origin
Pruning origin
URL: git@gitee.com:chuanru/remote---project.git
 * [pruned] origin/dev
 * [pruned] origin/function-1
 * [pruned] origin/function-2
[qcr@ecs-205826 remote---project]$ git branch -a
  function-1
  function-2
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
        这样就删除了那些远程仓库不存在的分支。对于本地仓库的删除,之前的课程已经学过了,大家可以自行从操作。
补充:
        对于已经使用完的本地分支,本地仓库就可以进行删除了。
[qcr@ecs-205826 remote---project]$ git branch -d function-1
Deleted branch function-1 (was e75c59c).
[qcr@ecs-205826 remote---project]$ git branch -d function-2
Deleted branch function-2 (was f2a379e).
[qcr@ecs-205826 remote---project]$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

到了这里,关于【Git原理与使用】-- 多人协作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Git 多人协作开发

    任务名称: 任务描述: 任务优先级:1(1最优先) 周期:10ms和1ms README.md为markdown语言编写的文件,可使用 typora 软件进行读写。 版本 时间 更新说明 修订者 V0.1 2023/10/27 base版本 在主库已经存在的情况下,日常操作流程如下: Git 全局设置

    2024年04月10日
    浏览(30)
  • Git分支——多人协作开发

    Git分支可以将主线任务(项目)分为若干个分支,一个或若干个人操控一个分支,在同一时间点各司其职,完成相对应的工作,各分支完成之后总汇在主线任务上,在最短的时间内完成项目需求,实现多人协作开发 多人协作开发不仅仅节省时间,还能防止互相干扰,每一个具

    2023年04月08日
    浏览(28)
  • Git的远程操作与多人协作

    \\\"爱在地图上剥落,我离孤单几公里~\\\"          我们目前所说、所学的内容(工作区、暂存区、版本库)都只是存在于本地上,也就是说你的一台机器上只有这么一个你维护的版本库。可是Git是一个分布式版本控制系统,这又是什么意思呢?         ——前言          可以

    2024年02月15日
    浏览(33)
  • 【掌握版本控制:Git 入门与实践指南】多人协作

                                                      🎬慕斯主页 : 修仙—别有洞天                                               ♈️ 今日夜电波: 泥中に咲く—ウォルピスカーター                                                      

    2024年03月16日
    浏览(43)
  • Git企业开发控制理论和实操-从入门到深入(六)|多人协作开发

    那么这里博主先安利一些干货满满的专栏了! 首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助。 高质量博客汇总 然后就是博主最近最花时间的一个专栏《Git企业开发控制理论和实操》希望大家多多关注!

    2024年02月11日
    浏览(39)
  • 【项目多人协作的困扰】git-cli 解决 git merge 合并时 lock 文件变化,忘记重新安装依赖的问题

    相信大家多多少少都遇到过,当主线分支的代码,合入到自己的分支的时候,如果这时候,主线中有一些 依赖的更新或者添加或者删除 ,如果合入之后,没有及时的 install 的话,项目启动的时候,可能就会报错! 使用教程 打开项目根目录,运行 随后就会在你的项目中开始

    2024年02月14日
    浏览(33)
  • 简单明了的Git教程 |Idea使用git|HbuilderX使用git|VSCode使用git|git常见问题|git协作规范|git命令~

    目录 一、Git的由来以及Git是什么 二、安装 三、使用前的配置 四、使用前需要知道的基础概念 4.1、四个工作区域 4.2、分支 4.3、\\\".gitignore\\\"文件是什么 4.4、\\\".git\\\"文件是什么 五、常用命令 5.1、初始化仓库 5.2、查看状态 5.3、将代码放到暂存区 5.4、将代码放到资源库 5.5、远程操

    2024年01月21日
    浏览(43)
  • github使用笔记及git协作常用命令

    1.Github有一个主库 ,每个人自己也有一个库,称为分支。 2.Github的协作流程 :先从主库fork出自己的分支, 然后进行代码的修改等操作, 操作完之后从本地库上推到自己的服务器分支,然后 服务器分支Pull Request到 主库。 3.本地仓库由git维护的三棵“树\\\"组成 :第1个是工作目

    2024年02月14日
    浏览(32)
  • Git学习笔记(第5章):Git团队协作机制

    目录 5.1 团队内协作 5.2 跨团队协作         Git进行版本控制都是在本地库操作的。若想使用Git进行团队协作,就必须借助代码托管中心。 问题引入 :成员1(大佬)利用Git在宿主机上初始化本地库,完成代码的整体框架,并添加到暂存区和提交本地库。此时,若成员1想借

    2024年01月20日
    浏览(35)
  • Git——协作开发

    介绍多种协作工作流,以及它们各自的优缺点;同时还会了解信任链的概念,签名标签、签名合并、签名提交的使用方法。 主要内容包括以下几个部分: 中心式和分布式工作流,裸版本库。 远程版本库和一次性单点协作管理。 推送、拉取请求以及交换补丁。 版本的编址——

    2024年02月20日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包