git基础-(5)-远程团队内部协作

一、主力开发登录github创建项目仓库

参考图片:

创建完后,拷贝仓库地址 比如 https://github.com/sky123/huashan.git

二、主力开发将本地库提交到github远程仓库

1、设置远程仓库别名

git remote -v 查看当前所有远程地址别名
git remote add [别名] [远程库地址]
git remote add rr https://github.com/i12dream/huashan.git
【rr 是 远程仓库 自己取的 别名,以后还是取 origin 好,这样方便统一】

cooldeMacBook-Pro:git-learn cool$ git remote add rr https://github.com/sky123/huashan.git
cooldeMacBook-Pro:git-learn cool$ git remote -v
rr https://github.com/sky123/huashan.git (fetch)
rr https://github.com/sky123/huashan.git (push)
cooldeMacBook-Pro:git-learn cool$

2、将本地库上传到远程服务器

git push [远程库地址别名] [远程库分支名]

### 上传做法:osx 有弹窗 获取密码,我拒绝了 然后就在 命令行 输入了用户名和密码
cooldeMacBook-Pro:git-learn cool$ git push rr master
Username for 'https://github.com': sky123
Password for 'https://[email protected]': 
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 471 bytes | 471.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/sky123/huashan/pull/new/master
remote: 
To https://github.com/sky123/huashan.git
 * [new branch]      master -> master
cooldeMacBook-Pro:git-learn cool$ 

三、协作开发下载修改并提交到远程

1、协作开发先clone到本地

git clone [远程库地址]   作用:
1、完整的把远程库下载到本地
2、创建origin 远程地址别名
3、初始化本地库

新建一个目录,直接 git clone 就好了。git clone 会新建一个 仓库名文件夹,里面项目已经下载好了,git已经初始化好了 ,远程地址别名也设置好了。
特别注意:远程库 别名这里是 origin 【所以下次都用orign 当做远程库别名好了】

cooldeMacBook-Pro:desktop cool$ git clone
...
..

### 特别注意:远程库 别名这里居然是 origin 【所以下次都用orign 当做远程库别名好了】
cooldeMacBook-Pro:huashan cool$ git remote -v
origin	https://github.com/sky123/huashan.git (fetch)
origin	https://github.com/sky123/huashan.git (push)
cooldeMacBook-Pro:huashan cool$

2、修改了一些文件后,就先提交到本地库

3、提交到远程库的一些问题

当协作开发直接 推送到远程库时,发现 新创建的用户 需要邮箱验证一下。

cooldeMacBook-Pro:huashan cool$ git push origin master
Username for 'https://github.com': skyman
Password for 'https://[email protected]': 
remote: You must verify your email address.
remote: See https://github.com/settings/emails.
fatal: unable to access 'https://github.com/sky123/huashan.git/': The requested URL returned error: 403
cooldeMacBook-Pro:huashan cool$

登录邮箱验证完后,再一次推送提交,发现无法授权 ,需要仓库创建者【主力开发】的邀请。

邀请怎么添加呢?


“主力开发”通过各种方式把邀请链接发送给“协作开发”,“协作开发”登录自己的 GitHub账号,访问邀请链接。

成为合作者后,协作开发就能提交到远程仓库了。
cooldeMacBook-Pro:huashan cool$ git push origin master

四、主力开发从远程仓库拉取并合并到本地

 pull=fetch+merge
 git fetch [远程库地址别名] [远程分支名]
 git merge [远程库地址别名/远程分支名]
 git pull [远程库地址别名] [远程分支名]

1、可以使用 fetch 和 merge  两步操作

远程仓库管理者【主力开发】 :拉取远程已经修改的仓库。rr 是 之前远程仓库地址的别名。

cooldeMacBook-Pro:git-learn cool$ git remote -v
rr	https://github.com/sky123/huashan.git (fetch)
rr	https://github.com/sky123/huashan.git (push)

cooldeMacBook-Pro:git-learn cool$ git fetch rr master
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 2), reused 4 (delta 2), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/sky123/huashan
 * branch            master     -> FETCH_HEAD
   4a234e2..97806e9  master     -> rr/master
cooldeMacBook-Pro:git-learn cool$ 

下载到本地的目录是 rr/master ,也就是说本地有 master  rr/master 两个分支。

cooldeMacBook-Pro:git-learn cool$ git checkout rr/master
Note: checking out 'rr/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 97806e9 令狐冲
cooldeMacBook-Pro:git-learn cool$

接下来就是合并操作了:【如果合并时出现冲突,可以根据分支冲突一文来处理】
cooldeMacBook-Pro:git-learn cool$ git merge rr/master

2、也可以使用 pull 操作 一步到位

pull 等于 fetch + merge
cooldeMacBook-Pro:git-learn cool$ git pull rr master

五、主力开发和协作开发同时提交到远程问题

两个开发者 同时向 远程库 提交的情况下:第二个 开发者 提交时 会报错 。

cooldeMacBook-Pro:huashan cool$ git push origin master
Username for 'https://github.com': skyman
Password for 'https://[email protected]': 
To https://github.com/sky123/huashan.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/sky123/huashan.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.
cooldeMacBook-Pro:huashan cool$ 

我们需要从远程库中将最新的版本下载下来,先合并 然后再提交!!!

cooldeMacBook-Pro:huashan cool$ git pull origin master
From https://github.com/sky123/huashan
 * branch            master     -> FETCH_HEAD
Auto-merging apple.txt
CONFLICT (content): Merge conflict in apple.txt
Automatic merge failed; fix conflicts and then commit the result.
cooldeMacBook-Pro:huashan cool$ vim apple.txt
cooldeMacBook-Pro:huashan cool$ git add apple.txt
cooldeMacBook-Pro:huashan cool$ git commit -m "conflict resolve"
[master e416c03] conflict resolve
cooldeMacBook-Pro:huashan cool$ git push origin master
Username for 'https://github.com': skyman
Password for 'https://[email protected]': 
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 687 bytes | 687.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/sky123/huashan.git
   ff6f1a1..e416c03  master -> master
cooldeMacBook-Pro:huashan cool$ 

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments