目录
1. 多人协作一
2. 多人协作二
3. 远程分支删除后,本地git brahch -a依旧能看到的解决方法
1. 多人协作一
目前,我们所完成的工作如下:
基本完成Git的所有本地库的相关操作,git基本操作,分支理解,版本回退,冲突解决等等
申请码云账号,将远端信息clone到本地,以及推送和拉取。
是时候干最重要的一件事情了,实现多人协作开发!为了做这件事情,我们需要先做一些准备工作。我们之前已经将项目clone到了指定目录,如;
aurora@wanghao:~/remote-gitcode$ pwd
/home/aurora/remote-gitcode
我们在windows环境下,再clone同一个项目仓库,来模拟和你一起协作开发的另一名小伙伴:
clone成功。
注意,我们这里是模拟了两个用户,实际开发中,每个用户都有自己的gitee/github账号,如果要多人进
行协同开发,必须要将用户添加进开发者,用户才有权限进行代码提交:
邀请用户:
到此,相当于有了两个用户,分别在linux和windows上针对于同项目进行协作开发,我们的准备工作到此结束。
目前,我们的仓库中只有一个master主分支,但在实际的项目开发中,在任何情况下其实都是不允许直接在master分支上修改代码的,这是为了保证主分支的稳定。所以在开发新功能时,常常会新建其他分支,供开发时进行迭代使用。
那么接下来,就让我们在gitee上新建dev远程分支供我们使用:
创建成功:
创建成功的远程分支是可以通过Git拉取到本地来,以实现完成本地开发工作。
我们可以使用两个命令来查看本地分支和远端分支有哪些?
aurora@wanghao:~/remote-gitcode$ git branch
* master
aurora@wanghao:~/remote-gitcode$ git branch -rorigin/HEAD -> origin/masterorigin/master
对于我们本地来讲,有一个本地master分支,其实还能看到远程的master分支。我们所有的操作是在本地的分支上进行操作的,这个时候不管是本地的dev分支还是远端的dev分支,我们都看不到,这个时候我们可以进行pull操作。
接下来让我们和另一名开发的小伙伴都将远程仓库进行一次拉取操作,并观察结果:
对于我们要操作的是:
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代表既打印本地又打印远程的
* masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/master
之前说的pull操作和push操作都是对于分支的操作,我们push必须要让两个分支之间建立连接,有了这个连接,才可以push,对于push操作,我们给它指定origin,指定了远程,也指定了推送的是哪个分支,这个时候其实是不需要建立连接的,那什么时候用到建立的连接呢?就是我们想要简写(git push)的时候,其他的分支也是一样的。本地master和远端master克隆的时候就自动建立连接,所以pull的时候会生效。
拉取后便可以看到远程的dev分支,接着切换到dev分支供我们进行本地开发。要说明的是,我们切换到的是本地的dev分支,根据示例中的操作,会将本地分支和远程分支的进行关系链接。
对于小伙伴要操作的是:
我们在dev分支上开发:
# 创建本地分支dev并和远程分支dev建立连接
aurora@wanghao:~/remote-gitcode$ git branch -a
* masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/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
* devmasterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/master# git branch -vv 选项查看连接
aurora@wanghao:~/remote-gitcode$ git branch -vv
* dev f777118 [origin/dev] mdmaster 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: aaa1 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.gitf777118..33980a7 dev -> dev
我们来看一下远端:
我们的小伙伴在dev分支上开发:
PS E:\git\remote-gitcode> git branch -a
* masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/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 mdmaster 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-gitcodef777118..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] mdmaster 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: bbb1 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抓下来,然后,在本地进行合并,并解决冲突,再推送。操作如下:
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
解决冲突,并重新推送:
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.txtno 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.git33980a7..2ed9b3d dev -> dev
此时,我们看到远端的码云已经能看到我们的新提交了!
由此,两名开发者已经开始可以进行协同开发了,不断的gitpull/addcommit/push,遇到了
冲突,就使用我们之前讲的冲突处理解决掉冲突。
对于你来说,要想看到小伙伴的代码,只需要pul1一下即可,例如:
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-gitcode33980a7..2ed9b3d dev -> origin/dev
Updating 33980a7..2ed9b3d
Fast-forwardfile.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、本地
# 保证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-forwardfile.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.gitf777118..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,可以进行以下操作:
# 新增本地分支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-1master#新增内容需求-创建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 func11 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, usegit 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 -adev
* feature-1masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/feature-1remotes/origin/master
我们来看一下远端:
我们的码云上也确实有了这个分支。
对于开发者2,可以进行以下操作:
# 查看所处的分支
PS E:\git\remote-gitcode> git branch -a
* devmasterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/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-gitcodef777118..2ed9b3d master -> origin/master* [new branch] feature-1 -> origin/feature-1
Updating f777118..2ed9b3d
Fast-forwardfile.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 fun21 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分支帮忙继续开发,要做的操作如下:
# 必须先拉去远端仓库内容
aurora@wanghao:~/remote-gitcode$ git branch -adev
* feature-1masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/feature-1remotes/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 -adev
* feature-1masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/feature-1remotes/origin/feature-2remotes/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命令来说有两种情况:
-
拉取分支内的内容
-
拉取仓库的内容
切换成功后,便可以看见feature-2分支中的function2文件了,接着就可以帮小伙伴进行开发:
# 继续开发
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 func21 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.git647d844..9ce71f8 feature-2 -> feature-2
查看远程状态,推送成功了:
这时,你的小伙伴已经修养的差不多,可以继续进行自己的开发工作,那么他首先要获取到你帮他开发的内容,然后接着你的代码继续开发。或者你已经帮他开发完了,那他也需要在自己的电脑上看看你帮他写的代码:
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-gitcode647d844..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-2PS E:\git\remote-gitcode> cat .\function2.txt
i am coding...
Pull无效的原因是小伙伴没有指定本地feature-2分支与远程origin/feature-2分支的链接,根据提示,设置feature-2和origin/feature-2的链接即可:
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-forwardfunction2.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 func21 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.git9ce71f8..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。
# 切换至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-gitcode2ed9b3d..569974a master -> origin/master9ce71f8..93125cc feature-2 -> origin/feature-2
Updating 2ed9b3d..569974a
Fast-forwardfunction2.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 isbranch
aurora@wanghao:~/remote-gitcode$ git branch -vvdev 2ed9b3d [origin/dev] merge dev
* feature-1 9f5eefd Merge branch 'master' into feature-1feature-2 9ce71f8 [origin/feature-2: behind 1] md func2master 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.gite8cbe22..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命令可以查看所有本地分支和远程分支,但发现很多在远程仓库已经删除的分支在本地依然可以看到。例如:
aurora@wanghao:~/remote-gitcode$ git branch -adev
* feature-1feature-2masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/feature-1remotes/origin/feature-2remotes/origin/master
使用命令git remote show origin,可以查看remote地址,远程分支,还有本地分支与之相对应关系等信息。
aurora@wanghao:~/remote-gitcode$ git remote show origin
* remote originFetch URL: git@gitee.com:Axurea/remote-gitcode.gitPush URL: git@gitee.com:Axurea/remote-gitcode.gitHEAD branch: masterRemote branches:master trackedrefs/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 devfeature-2 merges with remote feature-2master merges with remote masterLocal ref configured for 'git push':master pushes to master (local out of date)
此时我们可以看到那些远程仓库已经不存在的分支,根据提示,使用git remote prune origin命令:
aurora@wanghao:~/remote-gitcode$ git remote prune origin
Pruning origin
URL: git@gitee.com:Axurea/remote-gitcode.git* [pruned] origin/dev* [pruned] origin/feature-1* [pruned] origin/feature-2# 再次查看
aurora@wanghao:~/remote-gitcode$ git branch -adev
* feature-1feature-2masterremotes/origin/HEAD -> origin/masterremotes/origin/master# 删除本地分支
aurora@wanghao:~/remote-gitcode$ git branch -d dev
Deleted branch dev (was 2ed9b3d).
aurora@wanghao:~/remote-gitcode$ git branch -a
* feature-1feature-2masterremotes/origin/HEAD -> origin/masterremotes/origin/master