Git 多人协作(2)
继上次Git多人协作操作后,本次我们来用一个新的例子来带大家感受Git的多人协作过程。
本章目标:
目标:远程master分支下新增function1和function2文件
实现:由开发者1新增function1,由开发者2新增function2
条件:在不同分支下协作完成。
一.实操演练
不同于我们的Git多人协作(1),这个例子中一个功能独占一个分支。
1.用户1开发
在这里我们推荐创建远程分支,在这里演示在本地创建分支并推送到远程仓库。
首先我们在用户1的Linux机器上创建新分支feature-1
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git branch -a
* devmasterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/master
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git checkout -b feature-1
Switched to a new branch 'feature-1'
然后我们进行add-commit-push一系列操作。
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git add .
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git commit -m"add function1 "
[feature-1 ce07e1a] add function11 file changed, 2 insertions(+)create mode 100644 function1
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git push origin feature-1
Username for 'https://gitee.com': wjhwujiahao
Password for 'https://wjhwujiahao@gitee.com':
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), 288 bytes | 288.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 1a48b3a1
remote: Create a pull request for 'feature-1' on Gitee by visiting:
remote: https://gitee.com/wjhwujiahao/linux-fundamentals-learning/pull/new/wjhwujiahao:feature-1...wjhwujiahao:master
To https://gitee.com/wjhwujiahao/linux-fundamentals-learning.git* [new branch] feature-1 -> feature-1
注意这里,我们已经实现了本地的feature-1和远端仓库的origin/feature-1分支的链接。
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git branch -adev
* feature-1masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/feature-1remotes/origin/master
再查看远端,分支feature-1也成功创建。
那么此时的状态为:
2.用户2开发与突发情况
此时模拟用户2,我们切换到Windows的本地克隆仓库git中。
初始时我们查看分支,我们并不在master上,这时我们的master很有可能不是最新的,所以先pull
tips:当我们在进行一系列操作之前,可以先查看自己当前分支,如果不在master上,那么可能需要pull远程仓库更新。
PS D:\git\linux-fundamentals-learning> git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
PS D:\git\linux-fundamentals-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 (from 0)
Unpacking objects: 100% (3/3), 268 bytes | 38.00 KiB/s, done.
From https://gitee.com/wjhwujiahao/linux-fundamentals-learning* [new branch] feature-1 -> origin/feature-1
Already up to date.
然后我们在本地创建feature-2分支,编辑文件,并执行add,commit,push等一系列操作。
此时状态
此时,出现了一些意外:用户2事出有因无法继续开发,那么用户1就需要接手用户2当前的工作,完善后提交至远端。
我们此时就需要把feature-2分支pull到用户1的Linux主机上。
拉取仓库内容不需要建立链接,而拉取分支内容需要。
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-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 (from 0)
Unpacking objects: 100% (3/3), 264 bytes | 264.00 KiB/s, done.
From https://gitee.com/wjhwujiahao/linux-fundamentals-learning* [new branch] 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-1wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git branch -adev
* feature-1masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/feature-1remotes/origin/feature-2remotes/origin/master
如果要建立链接,可以这样做:
git checkout -b feature-2 origin/feature-2
此时的状态:
然后我们继续开发,并执行Git三板斧...
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git push
Username for 'https://gitee.com': wjhwujiahao
Password for 'https://wjhwujiahao@gitee.com':
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), 274 bytes | 274.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 ea4dfdb6
To https://gitee.com/wjhwujiahao/linux-fundamentals-learning.gitc9db693..06a7df6 feature-2 -> feature-2
此时,用户2恢复正常,可以继续开发。此时,我们只需要将远端的feature-2分支pull到主机2即可。注意这里是拉取分支内容,所以我们需要执行上面的链接操作。
PS D:\git\linux-fundamentals-learning> git branch --set-upstream-to=origin/feature-2 feature-2
继续在用户2主机上对function2进行开发。
PS D:\git\linux-fundamentals-learning> git add .
PS D:\git\linux-fundamentals-learning> git commit -m "md function2"
[feature-2 0e65943] md function21 file changed, 1 insertion(+)
PS D:\git\linux-fundamentals-learning> git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 32 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 283 bytes | 283.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 2b4ec5bc
To https://gitee.com/wjhwujiahao/linux-fundamentals-learning.git06a7df6..0e65943 feature-2 -> feature-2
执行push之后,我们就可以在远端的feature-1中看到用户1开发的function1,以及用户1和用户2协同开发的function2。之后我们只需要将这两个分支与master合并即可。
注意,一般情况下我们不建议直接将分支合并到master上,应该将一个分支合并到另一个分支,解决分支的冲突之后再对master进行合并,但这里不会出现冲突的情况,就直接进行合并了。
可以看到在这里并没有出现冲突
推送
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git push origin feature-1
Username for 'https://gitee.com': wjhwujiahao
Password for 'https://wjhwujiahao@gitee.com':
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), 308 bytes | 308.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag a84613b5
To https://gitee.com/wjhwujiahao/linux-fundamentals-learning.gitce07e1a..ceba15f feature-1 -> feature-1
在远端仓库提交合并申请单