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

目录

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

开发者一(Linux)

开发者二(Windous)

master合并

远端上的合并

本地上的合并

总结

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

开发者一(Linux)

开发者二(Windous)

master合并

合并function-2

合并function-1

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


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

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

使用Shift + 鼠标右键

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

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

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

目标:

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

实现:

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

条件:

        在一个分支下协作完成。

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

        创建一个名为 dev 的分支。

        此时我们需要明确的知道,远程仓库就有了两个分支: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

开发者二(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 pull/add/commit/push ,遇到了冲突,就使用我们之前讲的冲突处理解决掉冲突。并且对于我们来说,要想看到同事的代码,只需要 pull 一下即可。

master合并

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

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

远端上的合并

        PR(Pull Request)。

本地上的合并

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

开发人员一

        此时开发者一的本地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

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

总结

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

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

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

目标:

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

实现:

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

条件:

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

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

融汇贯通的理解:

        推荐选择第一个,创建远程分支,因为远程分支如基于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

开发者二(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'

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

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

        但天有不测风云,你的同事突然生病了,但需求还没开发完,需要我们帮他继续开发,于是他便把 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

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

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

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 中才算真正意义上的开发完毕。

合并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
        此时远程仓库的状态。

合并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

        此时远程仓库的状态。

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

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

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

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

[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
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Git是一个分布式版本控制系统,它由以下几个部分组成: 1. 本地仓库(Local Repository):存储在开发者电脑上的版本库,包含了完整的项目历史记录和版本信息,以及Git的核心功能。 2. 远程仓库(Remote Repository):存储在远程服务器上的版本库,通常用于多人协作开发或备份。开发者可以通过Git命令将本地仓库与远程仓库同步。 3. 提交(Commit):将修改后的文件保存到本地仓库中,每次提交都会生成一个唯一的版本号(commit hash)。 4. 分支(Branch):Git将每个版本看作一个节点,每个分支代表一个相对独立的版本线,可以在分支上进行开发和测试,最终将分支合并到主线上。 5. 合并(Merge):将两个分支合并成一个新的版本,Git会自动解决冲突,并生成一个新的提交。 6. 标签(Tag):用于标记某个特定版本,方便开发者和团队进行管理和跟踪。 7. 追踪(Tracking):Git会自动追踪文件的修改,并将其标记为已修改状态,以便提交到本地仓库。 8. 推送(Push):将本地仓库中的修改推送到远程仓库中,使得多人协作开发更加便捷。 9. 拉取(Pull):从远程仓库中拉取最新的修改到本地仓库中,以便进行开发和测试。 Git的系统架构基于分布式版本控制系统的思想,将本地仓库和远程仓库视为等价的节点,通过版本控制和协作来实现软件开发的高效管理。Git的设计原理是基于Unix/Linux操作系统的,采用了分层的文件结构和数据模型,使得Git具有高度的可扩展性和灵活性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

川入

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值