关闭

Git学习笔记——多人协作

165人阅读 评论(0) 收藏 举报
分类:
Git学习笔记——多人协作

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


多人环境创建

首先我们需要模拟一个多人环境。前面的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 PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

C:\Users\joe> ls
    Directory: C:\Users\joe
Mode                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): .git
C:\Users\joe> 
mkdir work
    //连接工作目录,并git init
    Directory: C:\Users\joe
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         5/12/2015   9:36 AM            work
C:\Users\joe> cd .\work
C:\Users\joe\work> git init
Initialized 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.git
C:\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.cn
C:\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 know
n hosts.
remote: Counting objects: 60, done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 60 (delta 13), reused 59 (delta 12), pack-reused 0
Receiving 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\work

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         5/12/2015   9:48 AM            learngit
C:\Users\joe\work [master +1 ~0 -0 !]> cd .\learngit
C:\Users\joe\work\learngit [master]>
ls

    Directory: C:\Users\joe\work\learngit
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         5/12/2015   9:48 AM         11 abc.c
-a---         5/12/2015   9:48 AM        164 readme.txt


C:\Users\joe\work\learngit [master]>

多人协作开始

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

你的工作如下:

C:\Users\joe\work> ls    //工作空间什么也没有
//克隆库的时候,我们只能克隆主分支的内容
C:\Users\joe\work> 
git clone git@github.com:joesGit15/
learngit
Cloning into 'learngit'...
Warning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of know
n 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 deltas

Resolving deltas: 100% (14/14), done.
Checking connectivity... done.
C:\Users\joe\work>
ls

    Directory: C:\Users\joe\work
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         5/12/2015   8:00 PM            learngit
C:\Users\joe\work> cd .\learngit
C:\Users\joe\work\learngit [master]>
git branch
//只有主分支
* master
C:\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\learngit
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         5/12/2015   8:01 PM         10 abc.c
-a---         5/12/2015   8:00 PM        164 readme.txt
C:\Users\joe\work\learngit [dev]> cat .\abc.c
I am dev
C:\Users\joe\work\learngit [dev]> 
cat .\abc.c
//修改内容如下:
I am dev
I am joe from window
C:\Users\joe\work\learngit [dev +0 ~1 -0]> git add abc.c
C:\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 status
On branch dev
Your branch is ahead of 'origin/dev' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean
C:\Users\joe\work\learngit [dev]> 
git push origin dev
//提交并推送dev分支
Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of know
n 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 -> dev
C:\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

learngit
ubuntu@myUbuntu:~/joe$ 
cd learngit/

ubuntu@myUbuntu:~/joe/learngit$ git branch
* master
ubuntu@myUbuntu:~/joe/learngit$ 
git checkout -b dev origin/dev
//获取远程库的dev分支
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
切换到一个新分支 'dev'
ubuntu@myUbuntu:~/joe/learngit$ git branch
* dev
  master
ubuntu@myUbuntu:~/joe/learngit$ ls
abc.c  readme.txt
ubuntu@myUbuntu:~/joe/learngit$ cat abc.c 
I am dev
ubuntu@myUbuntu:~/joe/learngit$ vi abc.c 
ubuntu@myUbuntu:~/joe/learngit$ 
cat abc.c
 //修改文件内容如下
I am joe ubuntu
I am dev
ubuntu@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.c
Merge made by the 'recursive' strategy.
 abc.c | 1 +
 1 file changed, 1 insertion(+)
 //查看文件内容,还真给合并了。(如果想修改也可以)
ubuntu@myUbuntu:~/joe/learngit$
cat
 abc.c
I am joe ubuntu
I am dev
I am joe from window
buntu@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推送就能成功!

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:37139次
    • 积分:525
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:85篇
    • 译文:1篇
    • 评论:1条