Linux git(15)----多人协作

三世轮回 提交于 2020-01-25 08:46:39
用到的新命令
1.git remote:查看远程库的信息,-v显示更详细的信息:
2.git push origin master:推送要指定本地分支,这样,Git就会把该分支推送到远程库对应的远程分支上
3.git checkout -b 分支名 origin/分支名:在本地创建和远程分支对应的分支
4.git branch --set-upstream 分支名 origin/分支名:建立本地分支和远程分支的关联
5.git pull:从远程抓取分支,如果有冲突,要先处理冲突。

1.git remote:
[root@VM_0_11_centos learn_git]# git remote 
origin
[root@VM_0_11_centos learn_git]# git remote -v
origin  https://github.com/TQ5911/learn_git.git (fetch)
origin  https://github.com/TQ5911/learn_git.git (push)

推送分支
2.git push origin master
[root@VM_0_11_centos learn_git]# git push origin master 
Username for 'https://github.com': TQ5911
Password for 'https://TQ5911@github.com': 
Counting objects: 14, done.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (12/12), 1.36 KiB | 0 bytes/s, done.
Total 12 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 1 local object.
To https://github.com/TQ5911/learn_git.git
   bee4f9e..2db2519  master -> master
[root@VM_0_11_centos learn_git]# git push origin dev  <==如果要推送其他分支,比如dev
Enter passphrase for key '/root/.ssh/id_rsa': 
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/TQ5911/learn_git/pull/new/dev
remote: 
To git@github.com:TQ5911/learn_git.git
 * [new branch]      dev -> dev
<==github上新增dev分支如图1


抓取分支
3.git clone
[root@VM_0_11_centos ~]# rm -rf learn_git  
[root@VM_0_11_centos ~]# ls
gitskills
[root@VM_0_11_centos ~]# git clone git@github.com:TQ5911/learn_git.git
Cloning into 'learn_git'...
Enter passphrase for key '/root/.ssh/id_rsa':     <==密码
remote: Enumerating objects: 52, done.
remote: Counting objects: 100% (52/52), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 52 (delta 15), reused 52 (delta 15), pack-reused 0
Receiving objects: 100% (52/52), 4.72 KiB | 0 bytes/s, done.
Resolving deltas: 100% (15/15), done.
[root@VM_0_11_centos ~]# ls
learn_git


4.在dev分支上开发,就必须创建远程origin的dev分支到本
[root@VM_0_11_centos ~]# cd learn_git/
[root@VM_0_11_centos learn_git]# git branch 
* master    <==从远程库clone时,默认情况下,你的小伙伴只能看到本地的master分支
[root@VM_0_11_centos learn_git]# git checkout -b dev origin/dev 
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'
[root@VM_0_11_centos learn_git]# git branch 
* dev
  master

5.在dev分支下推送新文件
[root@VM_0_11_centos learn_git]# touch new.txt
[root@VM_0_11_centos learn_git]# git add new.txt
[root@VM_0_11_centos learn_git]# git commit -m "add new.txt"
[dev 4af8ca7] add new.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 new.txt
[root@VM_0_11_centos learn_git]# git push origin dev     <== 推送,如图2
Enter passphrase for key '/root/.ssh/id_rsa': 
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 323 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:TQ5911/learn_git.git
   2db2519..4af8ca7  dev -> dev


6.你的小伙伴已经向origin/dev分支推送了他的提交,而碰巧你也对同样的文件作了修改,并试图推送
[root@VM_0_11_centos learn_git]# cat new.txt 
something
[root@VM_0_11_centos learn_git]# git add new.txt
[root@VM_0_11_centos learn_git]# git commit -m "add somethings"
[dev 0bce28f] add somethings
 1 file changed, 1 insertion(+)
[root@VM_0_11_centos learn_git]# git push origin dev 
To git@github.com:TQ5911/learn_git.git
 ! [rejected]        dev -> dev (non-fast-forward)
error: failed to push some refs to 'git@github.com:TQ5911/learn_git.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.


7.推送失败,因为你的小伙伴的最新提交和你试图推送的提交有冲突,解决办法也很简单,Git已经提示我们,先用git pull把最新的提交从origin/dev抓下来,然后,在本地合并,解决冲突,再推送:
[root@VM_0_11_centos learn_git]# git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
    git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
    git branch --set-upstream-to=origin/<branch> dev

8.git pull也失败了,原因是没有指定本地dev分支与远程origin/dev分支的链接,根据提示,设置dev和origin/dev的链接:
[root@VM_0_11_centos learn_git]# git branch --set-upstream-to=origin/dev dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.


9.再pull:
[root@VM_0_11_centos learn_git]# git pull
Auto-merging env.txt
CONFLICT (add/add): Merge conflict in env.txt
Automatic merge failed; fix conflicts and then commit the result.


10.这回git pull成功,但是合并有冲突,需要手动解决,解决的方法和分支管理中的解决冲突完全一样。解决后,提交,再push:
[root@VM_0_11_centos learn_git]# git commit -m "fix env conflict"
[dev 57c53ab] fix env conflict
[root@VM_0_11_centos learn_git]# git push origin dev
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 621 bytes | 621.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To github.com:michaelliao/learngit.git
   7a5e5dd..57c53ab  dev -> dev


11.拉取单个远程上的文件
[root@VM_0_11_centos learn_git]# ls
LICENSE  new.txt  readme.txt  text.txt    
[root@VM_0_11_centos learn_git]# rm new.txt    <==删除文件 
rm: remove regular file ‘new.txt’? y    
[root@VM_0_11_centos learn_git]# git checkout new.txt    <==重新拉取new.txt
[root@VM_0_11_centos learn_git]# ls
LICENSE  new.txt  readme.txt  text.txt

同样rm 删除文件夹,也可以用  1.git checkout 文件夹路径  2.被删除的文件夹目录下 git checkout .
3.被删除的文件夹目录下git checkout 文件夹名 4.git reset --hard HEAD



因此,多人协作的工作模式通常是这样:
1.首先,可以试图用git push origin <branch-name>推送自己的修改;
2.如果推送失败,则因为远程分支比你的本地更新,需要先用git pull抓取远程的新提交,试图合并;
3.如果合并有冲突,则解决冲突,并在本地提交;
4.没有冲突或者解决掉冲突后,再用git push origin <branch-name>推送就能成功!

如果git pull提示no tracking information,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream-to <branch-name> origin/<branch-name>。
这就是多人协作的工作模式,一旦熟悉了,就非常简单。



本地新建的分支如果不推送到远程,对其他人就是不可见的;

图1

图2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!