目录
一、邀请开发用户
二、新建远程分支
三、拉取远程分支
四、推送远程分支
五、合并远程分支
六、多分支协作
在windows环境下,再clone同⼀个项⽬仓库,来模拟⼀起协作开发的另⼀名⼩伙伴。
际开发中,每个⽤⼾都有⾃⼰的 gitee/github 账号,如果要多⼈进⾏协同开发,必须要将⽤⼾添加进开发者,⽤⼾才有权限进⾏代码提交:
至此相当于有了两个⽤⼾,分别在linux和windows上针对于同项⽬进⾏协作开发。
⽬前,我们的仓库中只有⼀个maste主分⽀,但在实际的项⽬开发中,在任何情况下其实都是不允许直接在master分⽀上修改代码的,这是为了保证主分⽀的稳定。所以在开发新功能时,常常会新建其他分⽀,供开发时进⾏迭代使⽤。
创建成功的远程分⽀是可以通过Git拉取到本地来,以实现完成本地开发⼯作。
接下来让我们和另⼀名开发的⼩伙伴都将远程仓库进⾏⼀次拉取操作,并观察结果:
# Linux系统
# git branch 是查看本地分支
# git branch -r 可查看远端分支
(base) [root@localhost git-learning]# git pull
来自 gitee.com:hdu-a-chao/git-learning
* [新分支] dev -> origin/dev
Already up-to-date.
(base) [root@localhost git-learning]# git branch -r
origin/HEAD -> origin/master
origin/dev
origin/master
(base) [root@localhost git-learning]#
# Window系统
C:\Users\pheonixFly\Gitee\git-learning>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/'...
From https://gitee.com/hdu-a-chao/git-learning
* [new branch] dev -> origin/dev
Already up to date.
C:\Users\pheonixFly\Gitee\git-learning>git branch -r
origin/HEAD -> origin/master
origin/dev
origin/master
C:\Users\pheonixFly\Gitee\git-learning>
拉取后便可以看到远程的dev分⽀,接着切换到dev分⽀供我们进⾏本地开发。
需要注意的是,我们切换到的是本地dev分支,在切换的时候将本地分支和远程分支相连接。
# Linux系统
# git branch -a 是列出所有本地分支和远程分支
(base) [root@localhost git-learning]# git checkout -b dev origin/dev
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
切换到一个新分支 'dev'
(base) [root@localhost git-learning]# git branch -a
* dev
master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/master
(base) [root@localhost git-learning]#
# Windows系统
C:\Users\pheonixFly\Gitee\git-learning>git checkout dev
Switched to a new branch 'dev'
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
C:\Users\pheonixFly\Gitee\git-learning>
# Linux系统
(base) [root@localhost git-learning]# vim file.txt
(base) [root@localhost git-learning]# cat file.txt
complete the first function!
(base) [root@localhost git-learning]# git add .
(base) [root@localhost git-learning]# git commit -m "first function"
[dev 25fed23] first function
1 file changed, 1 insertion(+)
create mode 100644 file.txt
(base) [root@localhost git-learning]# git push origin dev
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 282 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 0d4dcf8c
To [email protected]:hdu-a-chao/git-learning.git
8d78346..25fed23 dev -> dev
(base) [root@localhost git-learning]#
⾄此,我们已经将代码成功推送⾄码云,接下来假如你的⼩伙伴要和你协同开发,碰巧也要对file.txt⽂件作修改,并试图推送。
# Windows系统,推送失败
C:\Users\pheonixFly\Gitee\git-learning>type file.txt
complete the second function
C:\Users\pheonixFly\Gitee\git-learning>git add .
C:\Users\pheonixFly\Gitee\git-learning>git commit -m "second function"
[dev d1b29e4] second function
1 file changed, 1 insertion(+)
create mode 100644 file.txt
C:\Users\pheonixFly\Gitee\git-learning>git push origin dev
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/hdu-a-chao/git-learning.git
! [rejected] dev -> dev (fetch first)
error: failed to push some refs to 'https://gitee.com/hdu-a-chao/git-learning.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
C:\Users\pheonixFly\Gitee\git-learning>
推送失败,因为你的⼩伙伴的最新提交和你推送的提交有冲突,解决办法也很简单,Git已经提⽰我们,先⽤ git pull 把最新的提交从 origin/dev 抓下来,然后,在本地进⾏合并,并解决冲突,再推送。
# Windows系统
C:\Users\pheonixFly\Gitee\git-learning>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), 262 bytes | 37.00 KiB/s, done.
From https://gitee.com/hdu-a-chao/git-learning
8d78346..25fed23 dev -> origin/dev
CONFLICT (add/add): Merge conflict in file.txt
Auto-merging file.txt
Automatic merge failed; fix conflicts and then commit the result.
C:\Users\pheonixFly\Gitee\git-learning>type file.txt
<<<<<<< HEAD
complete the second function
=======
complete the first function!
>>>>>>> 25fed23cedd3bf5d229bc6ec6d7b3bdc22863b7b
C:\Users\pheonixFly\Gitee\git-learning>
# Windows系统,修改冲突文件,重新提交
C:\Users\pheonixFly\Gitee\git-learning>type file.txt
complete the first function!
complete the second function!
C:\Users\pheonixFly\Gitee\git-learning>git add file.txt
C:\Users\pheonixFly\Gitee\git-learning>git commit -m "merge dev"
[dev b81ee56] merge dev
C:\Users\pheonixFly\Gitee\git-learning>git push origin dev
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: 12, done.
Counting objects: 100% (12/12), done.
Delta compression using up to 16 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 839 bytes | 839.00 KiB/s, done.
Total 9 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 22432588
To https://gitee.com/hdu-a-chao/git-learning.git
25fed23..b81ee56 dev -> dev
C:\Users\pheonixFly\Gitee\git-learning>
# Linux系统,查看最新的远程分支版本的代码
(base) [root@localhost git-learning]# git pull
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 9 (delta 3), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), done.
来自 gitee.com:hdu-a-chao/git-learning
25fed23..b81ee56 dev -> origin/dev
更新 25fed23..b81ee56
Fast-forward
file.txt | 1 +
1 file changed, 1 insertion(+)
(base) [root@localhost git-learning]# cat file.txt
complete the first function!
complete the second function!(base) [root@localhost git-learning]#
两名开发者已经开始可以进⾏协同开发了,不断的 git pull/add/commit/push ,遇到了冲突,就使⽤我们之前讲的冲突处理解决掉冲突。
虽然我们是在分⽀上进⾏多⼈协作开发,但最终的⽬的是要将开发后的代码合并到master上去,让我们的项⽬运⾏最新的代码。
# Linux系统
# 1. 切换到 master 分支,pull 一下,保证本地 master 分支是最新内容
# 2. 切换到 dev 分支上合并 master 分支,避免合并冲突发生在 master 分支上
# 3. 切换到 master分支合并 dev分支
# 4. 将 master 分支推送到远程 origin/master 分支
(base) [root@localhost git-learning]# git checkout master
切换到分支 'master'
(base) [root@localhost git-learning]# git pull
Already up-to-date.
(base) [root@localhost git-learning]# git checkout dev
切换到分支 'dev'
(base) [root@localhost git-learning]# git merge master
Already up-to-date.
(base) [root@localhost git-learning]# git checkout master
切换到分支 'master'
(base) [root@localhost git-learning]# git merge dev
更新 8d78346..b81ee56
Fast-forward
file.txt | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 file.txt
(base) [root@localhost git-learning]# cat file.txt
complete the first function!
complete the second function!(base) [root@localhost git-learning]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 4 个提交。
# (使用 "git push" 来发布您的本地提交)
#
无文件要提交,干净的工作区
(base) [root@localhost git-learning]# git push origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag f44bd18c
To [email protected]:hdu-a-chao/git-learning.git
8d78346..b81ee56 master -> master
(base) [root@localhost git-learning]# git status
# 位于分支 master
无文件要提交,干净的工作区
(base) [root@localhost git-learning]#
总结⼀下,在同⼀分⽀下进⾏多⼈协作的⼯作模式通常是这样:
⼀般情况下,如果有多需求需要多⼈同时进⾏开发,是不会在⼀个分⽀上进⾏多⼈开发,⽽是⼀个需求或⼀个功能点就要创建⼀个 feature 分⽀。
现在同时有两个需求需要你和你的⼩伙伴进⾏开发,那么你们俩便可以各⾃创建⼀个分⽀来完成⾃⼰的⼯作。
# Linux系统
# 在本地 feature1 分支上编写 function1 并推送到远程 feature1 分支
(base) [root@localhost git-learning]# git branch -a
dev
* feature-1
master
remotes/origin/HEAD -> origin/master
remotes/origin/feature-1
remotes/origin/feature-2
remotes/origin/master
(base) [root@localhost git-learning]# vim function1
(base) [root@localhost git-learning]# cat function1
Done!
(base) [root@localhost git-learning]# git add function1
(base) [root@localhost git-learning]# git commit -m "add function1"
# 位于分支 feature-1
无文件要提交,干净的工作区
(base) [root@localhost git-learning]# git push origin feature-1
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 265 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag b946fab3
To [email protected]:hdu-a-chao/git-learning.git
b81ee56..747de46 feature-1 -> feature-1
(base) [root@localhost git-learning]#
# Windows系统
# 在本地 feature2 分支上编写 function2 并推送到远程 feature2 分支
C:\Users\pheonixFly\Gitee\git-learning>git checkout -b feature-2
Switched to a new branch 'feature-2'
C:\Users\pheonixFly\Gitee\git-learning>type function2
Done!
C:\Users\pheonixFly\Gitee\git-learning>git add function2
C:\Users\pheonixFly\Gitee\git-learning>git commit -m "add function2"
On branch feature-2
nothing to commit, working tree clean
C:\Users\pheonixFly\Gitee\git-learning>git push origin feature-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), 271 bytes | 271.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 554bf2b4
To https://gitee.com/hdu-a-chao/git-learning.git
b81ee56..5388bb8 feature-2 -> feature-2
C:\Users\pheonixFly\Gitee\git-learning>
此时,在本地,你看不⻅他新建的⽂档,他看不⻅你新建的⽂档。并且推送各⾃的分⽀时,并没有任何冲突,你俩互不影响。
正常情况下,你俩就可以在⾃⼰的分⽀上进⾏专业的开发了,直到各自的功能实现完毕就合并到远程master分支上。
但天有不测⻛云,你的⼩伙伴突然⽣病了,但需求还没开发完,需要你帮他继续开发,于是他便把 feature-2 分⽀名告诉你了。这时你就需要在⾃⼰的机器上切换到 feature-2 分⽀帮忙继续开发。
# Linux系统,帮助小伙伴开发 function2
# 1. 拉取远端仓库
# 2. 新建本地 feature-2 分支并关联 origin/feature-2
# 若不连接,使用 git push 简写格式会推送失败
# 3. 帮助小伙伴开发 function2 并推送
(base) [root@localhost git-learning]# 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.
来自 gitee.com:hdu-a-chao/git-learning
b81ee56..5388bb8 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
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/ feature-1
(base) [root@localhost git-learning]# git branch -a
dev
* feature-1
master
remotes/origin/HEAD -> origin/master
remotes/origin/feature-1
remotes/origin/feature-2
remotes/origin/master
(base) [root@localhost git-learning]# git checkout -b feature-2 origin/feature-2
分支 feature-2 设置为跟踪来自 origin 的远程分支 feature-2。
切换到一个新分支 'feature-2'
(base) [root@localhost git-learning]# ls
file1 file1.ini file1.so file.ini file.so file.txt function2 README.en.md README.md
(base) [root@localhost git-learning]# vim function2
(base) [root@localhost git-learning]# cat function2
Done!
Help Done!
(base) [root@localhost git-learning]# git add function2
(base) [root@localhost git-learning]# git commit -m "help done function2"
[feature-2 ead5633] help done function2
1 file changed, 1 insertion(+)
(base) [root@localhost git-learning]# git push
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 262 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag d943ed88
To [email protected]:hdu-a-chao/git-learning.git
5388bb8..ead5633 feature-2 -> feature-2
(base) [root@localhost git-learning]#
这时,你的⼩伙伴已经修养的差不多,可以继续进⾏⾃⼰的开发⼯作,那么他⾸先要获取到你帮他开发的内容,然后接着你的代码继续开发,或者你已经帮他开发完了,那他也需要在⾃⼰的电脑上看看你帮他写的代码。
# Windows系统
# 发现并没有 pull 到最新的代码
C:\Users\pheonixFly\Gitee\git-learning>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), 242 bytes | 26.00 KiB/s, done.
From https://gitee.com/hdu-a-chao/git-learning
5388bb8..ead5633 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
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/ feature-2
C:\Users\pheonixFly\Gitee\git-learning>type function2
Done!
C:\Users\pheonixFly\Gitee\git-learning>git status
On branch feature-2
nothing to commit, working tree clean
C:\Users\pheonixFly\Gitee\git-learning>
Pull⽆效的原因是⼩伙伴没有指定本地 feature-2 分⽀与远程 origin/feature-2 分⽀的链接,根据提⽰,设置feature-2和origin/feature-2的链接再重新拉取即可。
# Windows系统
C:\Users\pheonixFly\Gitee\git-learning>git branch --set-upstream-to=origin/feature-2 feature-2
Branch 'feature-2' set up to track remote branch 'feature-2' from 'origin'.
C:\Users\pheonixFly\Gitee\git-learning>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 5388bb8..ead5633
Fast-forward
function2 | 1 +
1 file changed, 1 insertion(+)
C:\Users\pheonixFly\Gitee\git-learning>type function2
Done!
Help Done!
C:\Users\pheonixFly\Gitee\git-learning>
⽬前,⼩伙伴的本地代码和远端保持严格⼀致。你和你的⼩伙伴可以继续在不同的分⽀下进⾏协同开发了。
各⾃功能开发完毕后,我们需要将代码合并到 master 中才算真正意义上的开发完毕。
# Windows系统
# 小伙伴率先开发完毕,开始 merge
C:\Users\pheonixFly\Gitee\git-learning>git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 4 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
C:\Users\pheonixFly\Gitee\git-learning>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 8d78346..b81ee56
Fast-forward
file.txt | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 file.txt
C:\Users\pheonixFly\Gitee\git-learning>git push origin master
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 [1.1.5]
remote: Set trace flag 0645149a
To https://gitee.com/hdu-a-chao/git-learning.git
b81ee56..ead5633 master -> master
C:\Users\pheonixFly\Gitee\git-learning>
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
C:\Users\pheonixFly\Gitee\git-learning>git merge feature-2
Updating b81ee56..ead5633
Fast-forward
function2 | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 function2
C:\Users\pheonixFly\Gitee\git-learning>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
C:\Users\pheonixFly\Gitee\git-learning>git push origin feature-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/'...
Everything up-to-date
C:\Users\pheonixFly\Gitee\git-learning>
当你的⼩伙伴将其代码 merge 到 master 后,这是你也开发完成了,也需要进⾏ merge 到 master 操作。
# Linux系统
(base) [root@localhost git-learning]# git checkout master
切换到分支 'master'
(base) [root@localhost git-learning]# git pull
来自 gitee.com:hdu-a-chao/git-learning
b81ee56..ead5633 master -> origin/master
更新 b81ee56..ead5633
Fast-forward
function2 | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 function2
(base) [root@localhost git-learning]# git checkout feature-1
切换到分支 'feature-1'
(base) [root@localhost git-learning]# git merge master
Merge made by the 'recursive' strategy.
function2 | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 function2
(base) [root@localhost git-learning]# ls
file1 file1.ini file1.so file.ini file.so file.txt function1 function2 README.en.md README.md
(base) [root@localhost git-learning]# git status
# 位于分支 feature-1
无文件要提交,干净的工作区
(base) [root@localhost git-learning]# git checkout master
切换到分支 'master'
(base) [root@localhost git-learning]# git merge feature-1
更新 ead5633..7111247
Fast-forward
function1 | 1 +
1 file changed, 1 insertion(+)
create mode 100644 function1
(base) [root@localhost git-learning]# ls
file1 file1.ini file1.so file.ini file.so file.txt function1 function2 README.en.md README.md
(base) [root@localhost git-learning]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 2 个提交。
# (使用 "git push" 来发布您的本地提交)
#
无文件要提交,干净的工作区
(base) [root@localhost git-learning]# git push origin master
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 307 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 7b48b099
To [email protected]:hdu-a-chao/git-learning.git
ead5633..7111247 master -> master
(base) [root@localhost git-learning]#
此时, feature-1 和 feature-2 分⽀对于我们来说就没⽤了,那么我们可以直接在远程仓库中将dev分⽀删除掉。
远程分支删除后,但是我么本地 pull 后还是可以看到那些远程仓库已经不存在的分⽀。
使⽤ git remote prune origin 删掉那些远程仓库不存在的分⽀。
# 当我们在Gitee服务器上删除了 feature-1 feature-2
(base) [root@localhost git-learning]# git pull
Already up-to-date.
(base) [root@localhost git-learning]# git branch -a
dev
feature-1
feature-2
* master
remotes/origin/HEAD -> origin/master
remotes/origin/feature-1
remotes/origin/feature-2
remotes/origin/master
(base) [root@localhost git-learning]# git remote prune origin
修剪 origin
URL:[email protected]:hdu-a-chao/git-learning.git
* [已删除] origin/feature-1
* [已删除] origin/feature-2
(base) [root@localhost git-learning]# git branch -a
dev
feature-1
feature-2
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
(base) [root@localhost git-learning]#