你的位置:首页 > ASP.net教程

[ASP.net教程]Git学习笔记(7)——多人协作


本文主要记录了,多人协作时,产生冲突时的解决情况.


多人环境创建

首先我们需要模拟一个多人环境。前面的Git的学习都是在Ubuntu上面,现在我们也搭建一个win环境吧。安装win环境下的Git,很简单,github.com官网下载,就像QQ一样,安装完以后,开始——所有程序——GitHub,Inc文件夹下的Git Shell打开即可。前面的Git命令在这里都可以的。ls你就会发现.ssh文件了,而且你登陆web的Git后,在Settings的SSH key里,已经自动添加了win的公钥。我们所做的就是

  1. 为主机设置邮箱和用户名
  2. 建立SSH连接
  3. 克隆远端的库
Windows PowerShellCopyright (C) 2009 Microsoft Corporation. All rights reserved.C:\Users\joe> ls  Directory: C:\Users\joeMode        LastWriteTime   Length Name----        -------------   ------ ----....d----     5/10/2015  9:13 PM      .ssh  //这里我只显示了2个有关的文件-a---     5/10/2015  9:13 PM    145 .gitconfig...C:\Users\joe> 
git remote
  //查看连接,发现没有连接fatal: Not a git repository (or any of the parent directories): .gitC:\Users\joe> 
mkdir work
  //连接工作目录,并git init  Directory: C:\Users\joeMode        LastWriteTime   Length Name----        -------------   ------ ----d----     5/12/2015  9:36 AM      workC:\Users\joe> cd .\workC:\Users\joe\work> git initInitialized empty Git repository in C:/Users/joe/work/.git/C:\Users\joe\work [master]> git remote//下面的应该换成你自己的git文件C:\Users\joe\work [master]> 
git remote add origin git@github.com:joesGit15/
learngit.gitC:\Users\joe\work [master]> 
git remote -v
  //已经有了连接origin git@github.com:joesGit15/learngit.git (fetch)origin git@github.com:joesGit15/learngit.git (push)//可以看到已经有了连接,然后设置邮箱和用户名C:\Users\joe\work [master]> 
git config --
global user.email little_star14@sina.cnC:\Users\joe\work [master]> 
git config --global user.name "joe-PC"
C:\Users\joe\work [master]> 
git clone git@github.com:joesGit15/learngit.git
//克隆自己的库Cloning into 'learngit'...Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of known hosts.remote: Counting objects: 60, done.remote: Compressing objects: 100% (34/34), done.remote: Total 60 (delta 13), reused 59 (delta 12), pack-reused 0Receiving objects: 100% (60/60), 4.87 KiB | 0 bytes/s, done.Resolving deltas: 100% (13/13), done.Checking connectivity... done.C:\Users\joe\work [master +1 ~0 -0 !]> ls//查看,成功  Directory: C:\Users\joe\workMode        LastWriteTime   Length Name----        -------------   ------ ----d----     5/12/2015  9:48 AM      learngitC:\Users\joe\work [master +1 ~0 -0 !]> cd .\learngitC:\Users\joe\work\learngit [master]>
ls
  Directory: C:\Users\joe\work\learngitMode        LastWriteTime   Length Name----        -------------   ------ -----a---     5/12/2015  9:48 AM     11 abc.c-a---     5/12/2015  9:48 AM    164 readme.txtC:\Users\joe\work\learngit [master]>


多人协作开始

多人环境配置好了,接下来你的好朋友就开始和你一起工作了。早上8点,你们开始工作了,同时从远程库克隆了开发分支dev,然后对同一个文件进行了修改。由于你比较麻利,完成以后,就推送到了远程库。而你的同事比较慢,在你推送以后,才推送。

你的工作如下:

C:\Users\joe\work> ls  //工作空间什么也没有//克隆库的时候,我们只能克隆主分支的内容C:\Users\joe\work> 
git clone git@github.com:joesGit15/
learngitCloning into 'learngit'...Warning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of known hosts.remote: Counting objects: 93, done.Receiving objects: 100% (93/93), 8.00 KiB | 0 bytes/s, done.remote: Compressing objects: 100% (57/57), done.Resolving deltas:  0% (0/14)Resolving deltas: 35% (5/14)remote: Total 93 (delta 14), reused 91 (delta 13), pack-reused 0Resolving deltasResolving deltas: 100% (14/14), done.Checking connectivity... done.C:\Users\joe\work>
ls
  Directory: C:\Users\joe\workMode        LastWriteTime   Length Name----        -------------   ------ ----d----     5/12/2015  8:00 PM      learngitC:\Users\joe\work> cd .\learngitC:\Users\joe\work\learngit [master]>
git branch
//只有主分支* masterC:\Users\joe\work\learngit [master]>
git checkout -b dev origin/dev
//获取远程库的dev分支Branch dev set up to track remote branch dev from origin.Switched to a new branch 'dev'C:\Users\joe\work\learngit [dev]>
ls
  Directory: C:\Users\joe\work\learngitMode        LastWriteTime   Length Name----        -------------   ------ -----a---     5/12/2015  8:01 PM     10 abc.c-a---     5/12/2015  8:00 PM    164 readme.txtC:\Users\joe\work\learngit [dev]> cat .\abc.cI am devC:\Users\joe\work\learngit [dev]> 
cat .\abc.c
//修改内容如下:I am devI am joe from windowC:\Users\joe\work\learngit [dev +0 ~1 -0]> git add abc.cC:\Users\joe\work\learngit [dev +0 ~1 -0]> 
git commit -m "joe win"
[dev 0338e7b] joe win 1 file changed, 1 insertion(+)C:\Users\joe\work\learngit [dev]> git statusOn branch devYour branch is ahead of 'origin/dev' by 1 commit. (use "git push" to publish your local commits)nothing to commit, working directory cleanC:\Users\joe\work\learngit [dev]> 
git push origin dev
//提交并推送dev分支Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known hosts.Counting objects: 7, done.Delta compression using up to 4 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 300 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@github.com:joesGit15/learngit  62f2b43..0338e7b dev -> devC:\Users\joe\work\learngit [dev]>

你同事不急不慢的完成任务也准备提交。

ubuntu@myUbuntu:~/joe$ ls//开始什么都没有ubuntu@myUbuntu:~/joe$ 
git clone git@github.com:joesGit15/learngit
//克隆远程的库正克隆到 'learngit'...remote: Counting objects: 93, done.remote: Compressing objects: 100% (57/57), done.remote: Total 93 (delta 14), reused 91 (delta 13), pack-reused 0接收对象中: 100% (93/93), 8.00 KiB | 0 bytes/s, 完成.处理 delta 中: 100% (14/14), 完成.检查连接... 完成。ubuntu@myUbuntu:~/joe$
ls
learngitubuntu@myUbuntu:~/joe$ 
cd learngit/
ubuntu@myUbuntu:~/joe/learngit$ git branch* masterubuntu@myUbuntu:~/joe/learngit$ 
git checkout -b dev origin/dev
//获取远程库的dev分支分支 dev 设置为跟踪来自 origin 的远程分支 dev。切换到一个新分支 'dev'ubuntu@myUbuntu:~/joe/learngit$ git branch* dev masterubuntu@myUbuntu:~/joe/learngit$ lsabc.c readme.txtubuntu@myUbuntu:~/joe/learngit$ cat abc.c I am devubuntu@myUbuntu:~/joe/learngit$ vi abc.c ubuntu@myUbuntu:~/joe/learngit$ 
cat abc.c
 //修改文件内容如下I am joe ubuntuI am devubuntu@myUbuntu:~/joe/learngit$ git add abc.c ubuntu@myUbuntu:~/joe/learngit$
git commit -m "joe ubuntu"
[dev f1fe313] joe ubuntu 1 file changed, 1 insertion(+)ubuntu@myUbuntu:~/joe/learngit$ git status位于分支 dev您的分支领先 'origin/dev' 共 1 个提交。 (使用 "git push" 来发布您的本地提交)无文件要提交,干净的工作区ubuntu@myUbuntu:~/joe/learngit$ 
git push origin dev
//提交以后,准备推送的时候,却出了问题To git@github.com:joesGit15/learngit ! [rejected]    dev -> dev (fetch first)error: 无法推送一些引用到 'git@github.com:joesGit15/learngit'提示:更新被拒绝,因为远程版本库包含您本地尚不存在的提交。这通常是因为另外提示:一个版本库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更提示:(如 'git pull ...')。提示:详见 'git push --help' 中的 'Note about fast-forwards' 小节。//根据提示,我们需要git pull这个命令,来查看冲突内容,有的时候,它会自动合并,有的时候需要手动来解决冲突(按照提示来就可以了)ubuntu@myUbuntu:~/joe/learngit$ git pull  remote: Counting objects: 3, done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0展开对象中: 100% (3/3), 完成.来自 github.com:joesGit15/learngit  62f2b43..0338e7b dev    -> origin/dev自动合并 abc.cMerge made by the 'recursive' strategy. abc.c | 1 + 1 file changed, 1 insertion(+) //查看文件内容,还真给合并了。(如果想修改也可以)ubuntu@myUbuntu:~/joe/learngit$
cat
 abc.cI am joe ubuntuI am devI am joe from windowbuntu@myUbuntu:~/joe/learngit$
git push origin dev
//然后推送,OK,推送成功了。对象计数中: 6, 完成.Delta compression using up to 4 threads.压缩对象中: 100% (4/4), 完成.写入对象中: 100% (6/6), 628 bytes | 0 bytes/s, 完成.Total 6 (delta 0), reused 0 (delta 0)To git@github.com:joesGit15/learngit  0338e7b..532f546 dev -> dev


总结

  1. 首先git push origin branch-name推送自己的修改;
  2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
  3. 如果合并有冲突,则解决冲突,并在本地提交;
  4. 没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!

还有就是,通过这个案例,我们要做事儿尽快,别落后了,落后了就会有麻烦,先下手为强,后下手遭殃。