目录
一、多人协作一
⽬前,我们所完成的⼯作如下:
1.基本完成Git的所有本地库的相关操作,git基本操作,分⽀理解,版本回退,冲突解决等等
2.申请码云账号,将远端信息clone到本地,以及推送和拉取。
我们之前已经将项⽬clone到了指定⽬录
cpp
[zl@VM-16-2-centos lesson/git_teaching]$ pwd
/home/zl/git_teaching
我们在windows环境下,再clone同⼀个项⽬仓库,来模拟和你⼀起协作开发的另⼀名⼩伙伴:
实际开发中,每个⽤⼾都有⾃⼰的gitee/github账号,如果要多⼈进⾏协同开发,必须要将⽤⼾添加进开发者,⽤⼾才有权限进⾏代码提交:
邀请⽤⼾:
到此,相当于有了两个⽤⼾,分别在linux和windows上针对于同项⽬进⾏协作开发,我们的准备⼯作到此结束。
⽬前,我们的仓库中只有⼀个master主分⽀,但在实际的项⽬开发中,在任何情况下其实都是不允许直接在master分⽀上修改代码的,这是为了保证主分⽀的稳定。所以在开发新功能时,常常会新建其他分⽀,供开发时进⾏迭代使⽤。
那么接下来,就让我们在gitee上新建dev远程分⽀供我们使⽤:
创建成功的远程分⽀是可以通过Git拉取到本地来,以实现完成本地开发⼯作。
接下来让我们和另⼀名开发的⼩伙伴都将远程仓库进⾏⼀次拉取操作,并观察结果
1.对于我们要操作的是:
cpp
[zl@VM-16-2-centos lesson/git_teaching]$ git pull
From gitee.com:hyb91/git_teaching
* [new branch] dev -> origin/dev
Already up to date.
# 注:之前讲的 git branch 其实只能查看本地分⽀,要查看远程分⽀需要加上-r选项。
# 但前提是要pull⼀下拉取最新的远端仓库,才能看到最新的内容。
[zl@VM-16-2-centos lesson/git_teaching]$ git branch -r //查看远程仓库后多少分支
origin/HEAD -> origin/master
origin/dev
origin/master
[zl@VM-16-2-centos lesson/git_teaching]$ git checkout -b dev origin/dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
Switched to a new branch 'dev'
[zl@VM-16-2-centos lesson/git_teaching]$ git branch -vv //可以查看本地的分支与远程仓库建立分支连接的情况
拉取后便可以看到远程的dev分⽀,接着切换到dev分⽀供我们进⾏本地开发。要说明的是,我们切换到的是本地的dev分⽀,根据⽰例中的操作,会将本地分⽀和远程分⽀的进⾏关系链接。
通过checkout就可以将本地的分支与远程的分支建立起连接,以后就可以直接使用push和pull操作了(不需要再加上orgin选项)
开发者2直接通过clone来建立与远程仓库的连接
1.对于⼩伙伴要操作的是:
现在,你和你的⼩伙伴就可以在 dev 上完成开发。
⾸先,让我们在 dev 分⽀上进⾏⼀次开发,并 push 到远程
cpp
[zl@VM-16-2-centos lesson/git_teaching]$ vim file.txt
[zl@VM-16-2-centos lesson/git_teaching]$ cat file.txt
hello git
complete the first function!
[zl@VM-16-2-centos lesson/git_teaching]$ git add file.txt
[zl@VM-16-2-centos lesson/git_teaching]$ git commit -m "first function"
[dev 305f78a] first function
1 file changed, 1 deletion(-)
[zl@VM-16-2-centos lesson/git_teaching]$ git push origin dev # 将dev分⽀推送到远端
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 287 bytes | 287.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:hyb91/git_teaching.git
cc3be59..305f78a dev -> dev
让我们来看看码云上⽬前仓库的状态:
⾄此,我们已经将代码成功推送⾄码云,接下来假如你的⼩伙伴要和你协同开发,碰巧也要对file.txt⽂件作修改,并试图推送
这时推送失败,因为你的⼩伙伴的最新提交和你推送的提交有冲突,解决办法也很简单,Git已经提⽰我们,先⽤ git pull 把最新的提交从 origin/dev 抓下来,然后,在本地进⾏合并,并解决冲突,再推送。
解决冲突,重新推送:
此时,我们看到远端的码云已经能看到我们的新提交了
由此,两名开发者已经开始可以进⾏协同开发了,不断的 git pull/add/commit/push ,遇到了冲突,就使⽤我们之前讲的冲突处理解决掉冲突
对于你来说,要想看到⼩伙伴的代码,只需要 pull ⼀下即可
cpp
[zl@VM-16-2-centos lesson/git_teaching]$ cat file.txt
hello git
complete the first function!
[zl@VM-16-2-centos lesson/git_teaching]$ git pull
Updating 305f78a..72c5345
Fast-forward
file.txt | 1 +
1 file changed, 1 insertion(+)
[zl@VM-16-2-centos lesson/git_teaching]$ cat file.txt
hello git
complete the first function!
complete the second function!
最后不要忘记,虽然我们是在分⽀上进⾏多⼈协作开发,但最终的⽬的是要将开发后的代码合并到master上去,让我们的项⽬运⾏最新的代码。接下来我们就需要做这件事情了:
cpp
# 切换⾄ master分⽀, pull ⼀下,保证本地的master是最新内容。
# 合并前这么做是⼀个好习惯
[zl@VM-16-2-centos lesson/git_teaching]$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
[zl@VM-16-2-centos lesson/git_teaching]$ git pull
Already up to date.
# 切换⾄ dev 分⽀, 合并 master 分⽀
# 这么做是因为如果有冲突,可以在dev分⽀上进⾏处理,⽽不是在在master上解决冲突。
# 这么做是⼀个好习惯
[zl@VM-16-2-centos lesson/git_teaching]$ git checkout dev
Switched to branch 'dev'
Your branch is up to date with 'origin/dev'.
[zl@VM-16-2-centos lesson/git_teaching]$ git merge master
Already up to date.
# 切换⾄ master 分⽀,合并 dev 分⽀
[zl@VM-16-2-centos lesson/git_teaching]$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
[zl@VM-16-2-centos lesson/git_teaching]$ git merge dev
Updating 7388a31..72c5345
Fast-forward
file.txt | 2 ++
1 file changed, 2 insertions(+)
[zl@VM-16-2-centos lesson/git_teaching]$ cat file.txt
hello git
complete the first function!
complete the second function!
# 将 master 分⽀推送⾄远端
[zl@VM-16-2-centos lesson/git_teaching]$ git status
On branch master
Your branch is ahead of 'origin/master' by 4 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
[zl@VM-16-2-centos lesson/git_teaching]$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:hyb91/git_teaching.git
7388a31..72c5345 master -> master
[zl@VM-16-2-centos lesson/git_teaching]$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
此时,查看远端仓库,master已经是最新代码了:
此时,dev分⽀对于我们来说就没⽤了,那么dev分⽀就可以被删除掉。我们可以直接在远程仓库中将dev分⽀删除掉:
总结
⾸先,可以试图⽤git push origin branch-name推送⾃⼰的修改;
如果推送失败,则因为远程分⽀⽐你的本地更新,需要先⽤git pull试图合并;
如果合并有冲突,则解决冲突,并在本地提交;
没有冲突或者解决掉冲突后,再⽤git push origin branch-name推送就能成功!
功能开发完毕,将分⽀merge进master,最后删除分⽀。
二、多人协作二
⼀般情况下,如果有多需求需要多⼈同时进⾏开发,是不会在⼀个分⽀上进⾏多⼈开发,⽽是⼀个需求或⼀个功能点就要创建⼀个 feature 分⽀。
现在同时有两个需求需要你和你的⼩伙伴进⾏开发,那么你们俩便可以各⾃创建⼀个分⽀来完成⾃⼰的⼯作。在上个部分我们已经了解了可以从码云上直接创建远程分⽀,其实在本地创建的分⽀也可以通过推送的⽅式发送到远端。在这个部分我们就来⽤⼀下这种⽅式。
1.对于你来说,可以进⾏以下操作
cpp
# 新增本地分⽀ feature-1 并切换
[zl@VM-16-2-centos lesson/git_teaching]$ git branch
dev
* master
[zl@VM-16-2-centos lesson/git_teaching]$ git checkout -b feature-1
Switched to a new branch 'feature-1'
# 新增需求内容-创建function1⽂件
[zl@VM-16-2-centos lesson/git_teaching]$ vim function1
[zl@VM-16-2-centos lesson/git_teaching]$ cat function1
Done!
# 将 feature-1 分⽀推送到远端
[zl@VM-16-2-centos lesson/git_teaching]$ git add function1
[zl@VM-16-2-centos lesson/git_teaching]$ git commit -m"add function1"
[feature-1 12ed0db] add function1
1 file changed, 1 insertion(+)
create mode 100644 function1
[zl@VM-16-2-centos lesson/git_teaching]$ git push origin feature-1
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 270.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'feature-1' on Gitee by visiting:
remote: https://gitee.com/hyb91/git_teaching/pull/new/zl:feature-1...hyb9
To gitee.com:hyb91/git_teaching.git
* [new branch] feature-1 -> feature-1
2.对于⼩伙伴来说,可以进⾏以下操作:
此时,在本地,你看不⻅他新建的⽂档,他看不⻅你新建的⽂档。并且推送各⾃的分⽀时,并没有任何冲突,你俩互不影响,⽤起来很舒服!!
再来看下远端码云上此时的状态:
对于你的feature-1分⽀:
对于⼩伙伴的feature-2分⽀:
正常情况下,你俩就可以在⾃⼰的分⽀上进⾏专业的开发了!
但是当feature-2中的需求还没开发完,需要你帮他继续开发,于是他便把feature-2分⽀名告诉你了。这时你就需要在⾃⼰的机器上切换到feature-2分⽀帮忙继续开发,要做的操作如下:
cpp
# 必须先拉取远端仓库内容
[zl@VM-16-2-centos lesson/git_teaching]$ git pull
...
From gitee.com:hyb91/git_teaching
305f78a..72c5345 dev -> origin/dev
* [new branch] feature-2 -> origin/feature-2
...
# 可以看到远程已经有了feature-2
[zl@VM-16-2-centos lesson/git_teaching]$ git branch -a
dev
* feature-1
master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/feature-1
remotes/origin/feature-2
remotes/origin/master
# 切换到feature-2分⽀上,可以和远程的feature-2分⽀关联起来,
# 否则将来只使⽤ git push 推送内容会失败
[zl@VM-16-2-centos lesson/git_teaching]$ 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'
[zl@VM-16-2-centos lesson/git_teaching]$ ls
a.so b.ini file.txt function2 README.en.md README.md
切换成功后,便可以看⻅feature-2分⽀中的function2⽂件了,接着就可以帮⼩伙伴进⾏开发:
cpp
# 继续开发
[zl@VM-16-2-centos lesson/git_teaching]$ vim function2
[zl@VM-16-2-centos lesson/git_teaching]$ cat function2
Done!
Help done!
# 推送内容
[zl@VM-16-2-centos lesson/git_teaching]$ git add function2
[zl@VM-16-2-centos lesson/git_teaching]$ git commit -m"modify function2"
[feature-2 1079ae7] modify function2
1 file changed, 2 insertions(+), 1 deletion(-)
[zl@VM-16-2-centos lesson/git_teaching]$ git push origin feature-2
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 262 bytes | 262.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:hyb91/git_teaching.git
e1233f1..1079ae7 feature-2 -> feature-2
查看远程状态,推送成功了:
这时,你的⼩伙伴已经修养的差不多,可以继续进⾏⾃⼰的开发⼯作,那么他⾸先要获取到你帮他开发的内容,然后接着你的代码继续开发。或者你已经帮他开发完了,那他也需要在⾃⼰的电脑上看看
你帮他写的代码:
Pull⽆效的原因是⼩伙伴没有指定本地feature-2分⽀与远程origin/feature-2分⽀的链接,根据提⽰,设置feature-2和origin/feature-2的链接即可:
⽬前,⼩伙伴的本地代码和远端保持严格⼀致。你和你的⼩伙伴可以继续在不同的分⽀下进⾏协同开发了。各⾃功能开发完毕后,不要忘记我们需要将代码合并到master中才算真正意义上的开发完毕。由于你的⼩伙伴率先开发完毕,于是开始merge :
此时远程仓库的状态:
当你的⼩伙伴将其代码 merge 到 master 后,这是你也开发完成了,也需要进⾏merge到master 操作,于是你:
cpp
# 切换⾄ master分⽀, pull ⼀下,保证本地的master是最新内容。
# 合并前这么做是⼀个好习惯
[zl@VM-16-2-centos lesson/git_teaching]$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
[zl@VM-16-2-centos lesson/git_teaching]$ git pull
From gitee.com:hyb91/git_teaching
72c5345..29006bd master -> origin/master
Updating 72c5345..29006bd
Fast-forward
function2 | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 function2
# 切换⾄ feature-1 分⽀, 合并 master 分⽀
# 这么做是因为如果有冲突,可以在feature-1分⽀上进⾏处理,⽽不是在在master上解决冲突。
# 这么做是⼀个好习惯
[zl@VM-16-2-centos lesson/git_teaching]$ git checkout feature-1
Switched to branch 'feature-1'
Your branch is up to date with 'origin/feature-1'.
[zl@VM-16-2-centos lesson/git_teaching]$ git merge master
Merge made by the 'recursive' strategy.
function2 | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 function2
[zl@VM-16-2-centos lesson/git_teaching]$ ls
a.so b.ini file.txt function1 function2 README.en.md README.md
# 1、由于feature-1分⽀已经merge进来了新内容,为了保证远程分⽀最新,所以最好push⼀下。
# 2、要 push 的另⼀个原因是因为在实际的开发中,master的merge操作⼀般不是由我们⾃⼰在本地进
# 其他⼈员或某些平台merge时,操作的肯定是远程分⽀,所以就要保证远程分⽀的最新。
# 3、如果 merge 出现冲突,不要忘记需要commit才可以push!!
[zl@VM-16-2-centos lesson/git_teaching]$ git status
On branch feature-1
Your branch is ahead of 'origin/feature-1' by 4 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
[zl@VM-16-2-centos lesson/git_teaching]$ git push origin feature-1
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 299 bytes | 299.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:hyb91/git_teaching.git
ea75a35..4b4c3d4 feature-1 -> feature-1
# 切换⾄ master 分⽀,合并 feature-1 分⽀
[zl@VM-16-2-centos lesson/git_teaching]$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
[zl@VM-16-2-centos lesson/git_teaching]$ git merge feature-1
Updating 29006bd..4b4c3d4
Fast-forward
function1 | 1 +
1 file changed, 1 insertion(+)
create mode 100644 function1
[zl@VM-16-2-centos lesson/git_teaching]$ ls
a.so b.ini file.txt function1 function2 README.en.md README.md
# 将 master 分⽀推送⾄远端
[zl@VM-16-2-centos lesson/git_teaching]$ 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
[zl@VM-16-2-centos lesson/git_teaching]$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:hyb91/git_teaching.git
29006bd..4b4c3d4 master -> master
[zl@VM-16-2-centos lesson/git_teaching]$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
此时远程仓库的状态:
此时,feature-1和 feature-2分⽀对于我们来说就没⽤了,那么我们可以直接在远程仓库中将dev分⽀删除掉:
三、 远程分⽀删除后,本地git branch -a依然能看到的解决办法
当前我们已经删除了远程的⼏个分⽀,使⽤ git branch -a 命令可以查看所有本地分⽀和远程分⽀,但发现很多在远程仓库已经删除的分⽀在本地依然可以看到。例如:
cpp
[zl@VM-16-2-centos lesson/git_teaching]$ git pull
Already up to date.
[zl@VM-16-2-centos lesson/git_teaching]$ git branch -a
dev
feature-1
feature-2
* master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/feature-1
remotes/origin/feature-2
remotes/origin/master
使⽤命令git remote show origin ,可以查看remote地址,远程分⽀,还有本地分⽀与之相对应关系等信息
cpp
[zl@VM-16-2-centos lesson/git_teaching]$ git remote show origin
* remote origin
Fetch URL: git@gitee.com:hyb91/git_teaching.git
Push URL: git@gitee.com:hyb91/git_teaching.git
HEAD branch: master
Remote branches:
master tracked
refs/remotes/origin/dev stale (use 'git remote prune' to remove)
refs/remotes/origin/feature-1 stale (use 'git remote prune' to remove)
refs/remotes/origin/feature-2 stale (use 'git remote prune' to remove)
Local branches configured for 'git pull':
dev merges with remote dev
feature-1 merges with remote feature-1
feature-2 merges with remote feature-2
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
此时我们可以看到那些远程仓库已经不存在的分⽀,根据提⽰,使⽤ git remote prune origin命令:
cpp
[zl@VM-16-2-centos lesson/git_teaching]$ git remote prune origin
Pruning origin
URL: git@gitee.com:hyb91/git_teaching.git
* [pruned] origin/dev
* [pruned] origin/feature-1
* [pruned] origin/feature-2
[zl@VM-16-2-centos lesson/git_teaching]$ git branch -a
dev
feature-1
feature-2
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
这样就删除了那些远程仓库不存在的分⽀。