Git学习笔记(7)——多人协作

时间:2021-11-19 20:49:58

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


多人环境创建

首先我们需要模拟一个多人环境。前面的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) Microsoft Corporation. All rights reserved. C:\Users\joe> ls
Directory: C:\Users\joe
Mode LastWriteTime Length Name
---- ------------- ------ ----
....
d---- // : PM .ssh //这里我只显示了2个有关的文件
-a--- // : PM .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---- // : 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: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta ), pack-reused
Receiving objects: % (/), 4.87 KiB | bytes/s, done.
Resolving deltas: % (/), done.
Checking connectivity... done.
C:\Users\joe\work [master + ~ - !]> ls//查看,成功 Directory: C:\Users\joe\work Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- // : AM learngit
C:\Users\joe\work [master + ~ - !]> cd .\learngit
C:\Users\joe\work\learngit [master]>

ls

    Directory: C:\Users\joe\work\learngit
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- // : AM abc.c
-a--- // : AM 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: , done.
Receiving objects: % (/), 8.00 KiB | bytes/s, done.
remote: Compressing objects: % (/), done.Resolving deltas: % (/)
Resolving deltas: % (/)
remote: Total (delta ), reused (delta ), pack-reused 0Resolving deltas Resolving deltas: % (/), done.
Checking connectivity... done.
C:\Users\joe\work>

ls

    Directory: C:\Users\joe\work
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- // : 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--- // : PM abc.c
-a--- // : PM 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 + ~ -]> git add abc.c
C:\Users\joe\work\learngit [dev + ~ -]>

git commit -m "joe win"

[dev 0338e7b] joe win
file changed, insertion(+)
C:\Users\joe\work\learngit [dev]> git status
On branch dev
Your branch is ahead of 'origin/dev' by 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: , done.
Delta compression using up to threads.
Compressing objects: % (/), done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
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: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta ), pack-reused
接收对象中: % (/), 8.00 KiB | bytes/s, 完成.
处理 delta 中: % (/), 完成.
检查连接... 完成。
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
file changed, insertion(+)
ubuntu@myUbuntu:~/joe/learngit$ git status
位于分支 dev
您的分支领先 'origin/dev' 共 个提交。
(使用 "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: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta ), pack-reused
展开对象中: % (/), 完成.
来自 github.com:joesGit15/learngit
62f2b43..0338e7b dev -> origin/dev
自动合并 abc.c
Merge made by the 'recursive' strategy.
abc.c | +
file changed, 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,推送成功了。
对象计数中: , 完成.
Delta compression using up to threads.
压缩对象中: % (/), 完成.
写入对象中: % (/), bytes | bytes/s, 完成.
Total (delta ), reused (delta )
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推送就能成功!

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