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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pulong0748/article/details/82178758

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

从远程仓库克隆

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

$ git  clone [email protected]: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 '[email protected]: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

总结:

  1. 先从远程库拉取最新的代码
  2. 当推送自己代码之前,再次拉取远程库代码,如果有冲突,先解决冲突,再在本地提交。

    未完。。。。。。。。。。。。。。。

猜你喜欢

转载自blog.csdn.net/pulong0748/article/details/82178758