史上最简单的git教程|第十篇:git团队协作

     上一篇我们讲了一些分支管理常用的命令,那么假如我们多个人一起开发,该如何稳健的推进工作任务呢。

从远程仓库克隆

     首先我们需要从远程仓库克隆到本地仓库:

$ git  clone git@github.com:gxx628/tb.git
Cloning into 'tb'...
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (9/9), done.

     我们此时打印一下本地仓库的日志,可以发现我们在远程仓库做了三件事,初始化仓库、创建test文件、在test文件中添加了bbb,并且可以看到本地仓库的HEAD指向本地master分支:

$ git log --pretty=oneline --decorate
b63310c9096aeaca57c12cfb1b4705db8693fd05 (HEAD -> master, origin/master, origin/HEAD) 在远程库添加了bbb
2b035e517706c49235eb2b46f8b2294f20793b9a Create test
0daaaf654f2a4583eb0b647dc9dd5f88533ea079 Initial commit

     我们也可以查看一下远程库的信息:

$ git  remote -v
origin  https://github.com/gxx628/tb.git (fetch)
origin  https://github.com/gxx628/tb.git (push)

     那么现在我在本地库进行开发 ,我想删除test文件,怎么同步到远程仓库,请注意这里origin代表的就是你克隆过来的远程仓库,并且记住 master分支是默认的分支,如果你要提交到其他分支,请改其他分支的名字

$ git push origin master
Counting objects: 2, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 251 bytes | 251.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:gxx628/touchGit-remote.git
   1dd68e7..d817d9d  master -> master

     好了现在我们学会基本的一些推、拉操作,来模拟一个场景A和B同时开发:
A:修改test文件,但是没有push到远程库上:

 git  add .
$ git commit -m "我B上对test新增内容aaa"
[master adb3a6e] 我在B上对test新增内容aaa

     此时B:直接删除了test文件,并且push到远程库上,
     接着A突然回想到自己没有push到远程库上,所以开始push:

$ git push origin master
To github.com:gxx628/touchGit-remote.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:gxx628/touchGit-remote.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.

     像这种你push出错了,一般是其他协作的人已经提交了新的代码,可能存在着冲突,所以你可以先拉取最新的代码:

$ git  pull
remote: Counting objects: 2, done.
remote: Compressing objects: 100% (1/1), done.
Unpacking objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
From github.com:gxx628/touchGit-remote
   d817d9d..1d1267c  master     -> origin/master
CONFLICT (modify/delete): test.txt deleted in 1d1267c9135977c75e89144c78e59a148                                                                                                                                                                                               df605af and modified in HEAD. Version HEAD of test2.txt left in tree.
Automatic merge failed; fix conflicts and then commit the result.

     果然 ,上面提示B把test文件删除了,难怪我们向远程库推不上去代码,所以我们以后要推代码的时候,一定要先从远程库拉取最新的代码再推送自己代码,解决冲突之后再次提交,终于可以了:

$ git push origin  master
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 543 bytes | 181.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:gxx628/touchGit-remote.git
   1d1267c..e0755c1  master -> master

     现在模拟第二种情景:远程项目分给两个人开发A和B,让他们从远程拉取代码,并自己创建一个bug分支去修改这个远程项目的bug ,A新建b1分支,B新建b2分支:
A接下来的动作:

1:先 git clone https://github.com/gxx628/lesson2.git 
2: git checkout -b bug1
3: vim hello.world
4:git add . 
5:git commit -m "在A电脑的bug1分支进行了修改"
6:git checkout master
7:git merge bug1
8:git push origin master

     B接下来的动作同A:

1:先 git clone https://github.com/gxx628/lesson2.git 
2: git checkout -b bug2
3: vim hello.world
4:git add . 
5:git commit -m "在B电脑的bug2分支进行了修改"
6:git checkout master
7:git merge bug2
8:git push origin master

     但是A 在B之前push到远程仓库,所以此时B开始push会报错:

remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/gxx628/lesson2
   9431ea3..d1fe368  master     -> origin/master
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

     可以看出是要解决冲突了,先别急着解决,先打印看看状态,可以一目了然的知道同时修改了helloword:

$ git status
On branch master
Your branch and 'origin/master' 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:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

     此时我们再从远程拉取最新代码,看看冲突的地方:

git pull 
cat hello.txt
<<<<<<< HEAD
B电脑上新建了b2分支,并修改了hello 文件
=======
在A电脑上对b1分支进行了修改
>>>>>>> d1fe368fad25c4aecc14c7fba147281abeca3bbe

     接着我们就可以跟A去商量如何解决冲突了。


总结:

  1. 先从远程库拉取最新的代码
  2. 当推送自己代码之前,再次拉取远程库代码,如果有冲突,先解决冲突,再在本地提交。
  3. 假如你没有新建分支,请记住修改了文件一定要add commit ,假如你新建了分支,请记住一定要add commit merge千万记住,很多错误都来源于你这几步操作没有很好的完成。

上一篇:史上最简单的git教程|第九篇:分支管理工具

如果有小伙伴觉得我写的不错的话可以关注一下我的博客,我会一直持续更新,也可以支持一下我的公众号哦:java架构师小密圈,会分享架构师所必须深入研究的技术,比如netty,分布式,性能优化,spring源码分析,mybatis源码分析,等等等,同时还会分享一些赚钱理财的小套路哦,欢迎大家来支持,一起学习成长,程序员不仅仅是搬瓦工!
公众号:分享系列好文章
java架构师小密圈

交流群:群友互相分享资料
java架构师小密圈

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mindcarver

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值