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

news2024/9/25 17:13:12

目录

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/731532.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SQL Server数据库忘记了sa密码怎么办 亲测真的可用

我们在安装SQL Server数据库时&#xff0c;往往选择混合模式安装&#xff0c;在这里我们可以设置sa密码。混合模式安装后&#xff0c;我们可以通过Windows身份验证和SQL Server身份验证两种方式登陆。 如果sa密码忘记了&#xff0c;我们就无法用SQL Server身份验证登陆了。 那么…

【高性能、高并发】页面静态化解决方案-OpenResty

OpenResty OpenResty介绍 OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台&#xff0c;其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项 用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关 OpenResty通过汇聚各种设计精良的…

Python应用实例(一)外星人入侵(五)

外星人入侵&#xff08;五&#xff09; 1.项目回顾2.创建第一个外星人2.1 创建Alien类2.2 创建Alien实例 3.创建一群外星人3.1 确定一行可容纳多少个外星人3.2 创建一行外星人3.3 重构_create_fleet()3.4 添加行 在游戏《外星人入侵》中添加外星人。我们将首先在屏幕上边缘附近…

RHCE中级项目

一、项目需求 1、在 bbs.example.com 主机上创建 Discuz 论坛&#xff0c;数据库服务器使用 db.example.com 主机的 bbs 数据库实例&#xff0c;该实例由 MySQL数据库软件提供服务。 2、在 ntp.example.com 主机上创建 NTP 服务&#xff0c;该服务由 Chronyd软件提供服务&…

GoLang网络编程:HTTP服务端之底层原理与源码分析——http.HandleFunc()、http.ListenAndServe()

一、启动 http 服务 import ("net/http" ) func main() {http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {w.Write([]byte("ping...ping..."))})http.ListenAndServe(":8999", nil) }在 Golang只需要几行代…

第三章 SSD存储介质:闪存 3.4

3.4 闪存数据完整性 可采用以下数据完整性的技术确保用户数据不丢失&#xff1a; &#xff08;1&#xff09;ECC纠错&#xff1b; &#xff08;2&#xff09;RAID数据恢复&#xff1b; &#xff08;3&#xff09;重读&#xff08;Read Retry&#xff09;&#xff1b; &#xff…

C/C++指针从0到99(详解)

目录 一&#xff0c;指针的基础理解 二&#xff0c;指针的基本使用 三&#xff0c;为什么要用指针 四&#xff0c;指针与数组的联系 五&#xff0c;指针的拓展使用 1&#xff09;指针数组 2)数组指针 3&#xff09;函数指针 结构&#xff1a;返回类型 &#xff08;*p)…

我国新能源汽车存量已突破1620万辆,登记数量创历史新高

根据公安部发布的最新数据&#xff0c;截至2023年6月底&#xff0c;全国的机动车数量达到4.26亿辆&#xff0c;其中汽车数量为3.28亿辆&#xff0c;新能源汽车数量为1620万辆。与此同时&#xff0c;机动车驾驶人口达到5.13亿人&#xff0c;其中汽车驾驶人口为4.75亿人。在2023年…

Zabbix监控软件 Linux外多平台监控【Windows JAVA SNMP】

在之前的博客中&#xff0c;已经介绍了zabbix的安装&#xff0c;配置&#xff0c;以及如何用zabbix监控Linux服务器。这篇博客则介绍zabbix监控的其他几种方式&#xff08;Windows服务器 Java应用 SNMP&#xff09;。 -------------------- Zabbix 监控 Windows 系统 ---------…

传统图像处理之图像美化——图像金字塔

代码实战&#xff1a;图像融合 我将肯巴.沃克和约翰.沃尔融合在了一起。 具体见注释。 import numpy as np import cv2 import scipy.ndimage as ndimageimg cv2.imread("1.png")img1 cv2.imread("2.png")imgcv2.resize(img,(192,192)) img1cv2.resiz…

JVM调优的相关命令

在进行JVM调试时&#xff0c;熟练使用Linux和Java命令是非常重要的&#xff0c;有利于我们快速发现问题&#xff0c;并进行解决。平时常用的命令有&#xff1a;Linux的Top、Top -Hp等&#xff1b;Java的Jps、Jinfo、Jstack、Jstat、Jmap、Jhat等。下面&#xff0c;我将逐一介绍…

【技能实训】Day01

文章目录 任务1 项目准备一、开发环境二、系统简介三、项目创建 任务2【任务2.1】菜单项设计及其测试【任务2.2】使用数组存储采集的数据【任务2.3】控制显示采集的数据 任务1 项目准备 一、开发环境 1.JDK8下载及其环境变量配置(JDK8以上版本) 2.IDE &#xff1a;Eclipse 或…

多边形连接器PolygonConnector

PolygonConnector将多个多边形连接在一起 PolygonConnectorTest用于测试PolygonConnector Polygon test_square; Polygon test_square_around

关于IIS安全设置http能访问https不能访问的解决方案

最近折腾IIS&#xff0c;发现网站的http能访问但是https不能访问。 我确认所有关于HTTPS的配置我都配置正确了&#xff0c;结果还是不能访问&#xff0c;一番折腾发现&#xff0c;服务器本身的防火墙和阿里云服务器的安全组规则不是一回事。改完防火墙也没有用&#xff0c;重要…

【前端】网页开发精讲与实战 HTML Day 1

&#x1f680;Write In Front&#x1f680; &#x1f4dd;个人主页&#xff1a;令夏二十三 &#x1f381;欢迎各位→点赞&#x1f44d; 收藏⭐️ 留言&#x1f4dd; &#x1f4e3;系列专栏&#xff1a;前端 &#x1f4ac;总结&#xff1a;希望你看完之后&#xff0c;能对你有…

OpenCV使用ellipse()函数来绘制一个椭圆

/*** void ellipse(* InputOutputArray img, // 图像* Point center, // 椭圆原心* Size axes, // 椭圆x轴长度的一半,y轴长度的一半* double angle, // 椭圆旋转角度* double startAngle, // 起始角度* double endAngle, // 终止角度* const Scalar& color, // 椭圆颜色*…

整数序列(山东大学考研机试题)

整数序列 题目链接:3717. 整数序列 - AcWing题库 /* 纯暴力 刚开始以为挺难的结果是个暴力题 */ #include<iostream> using namespace std; int main() {int n;cin>>n;int f0;for(int i1;i<n/2;i){int sum 0;for(int ji;j<n/21;j){sumj;if(sumn){f1;for(int…

手机怎么设置四小时后提醒我休息,防止疲劳驾驶?

很多开车的人都知道&#xff0c;连续驾驶4个小时后需要至少休息20分钟&#xff0c;否则很容易出现疲劳驾驶的情况。但在开车的时候需要专注&#xff0c;很容易忘记按时休息&#xff0c;手机怎么设置四小时后提醒我休息&#xff0c;防止疲劳驾驶呢&#xff1f; 其实使用一款支持…

开放式耳机是什么意思?开放式耳机和封闭式耳机哪个好?

开放式耳机相比传统入耳式耳机&#xff0c;开放式耳机不会堵塞耳道&#xff0c;使用时可以开放双耳&#xff0c;不影响与他人的正常交流。还有很多朋友问开放式耳机和封闭式耳机哪个好&#xff1f;开放式耳机有哪些推荐好&#xff1f;等问题。 开放式耳机作为一种新兴的蓝牙耳…

String的hashCode为什么选择31作为乘子?

目录 hashCode()源码val[i] 是中文怎么办&#xff1f;如果是中文char的整数值是多少&#xff1f;注释中的计算方法为什么不用更大的数&#xff0c;比如101作为乘数&#xff1f;为啥用31了&#xff1f; hashCode()源码 先看下String类的源码&#xff0c;31出现在hashCode()方法…