当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且远程仓库的默认名称是origin。
1. 查看远程库的信息
$ git remote
origin
或者用 git remote -v 显示更详细的信息
$ git remote -v
origin git@github.com:maping/learngit.git (fetch)
origin git@github.com:maping/learngit.git (push)
上面显示了可以抓取和推送的origin的地址。如果没有推送权限,就看不到push的地址。
2. 推送分支
推送分支就是把该分支上的所有本地提交推送到远程库。
推送时,要指定本地分支,这样,Git就会把该分支推送到远程库对应的远程分支上。
$ git push origin master
如果要推送其他分支,比如dev,就改成:
$ git push origin dev
那么,实际使用中,哪些分支需要推送呢?
master分支是主分支,因此要时刻与远程同步。
dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步。
bug分支只用于在本地修复bug,就没必要推到远程了。
feature分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发。
3. 抓取分支
多人协作时,大家都会往master和dev分支上推送各自的修改。
现在,模拟一个你的小伙伴,可以在另一台电脑(注意要把SSH Key添加到GitHub)或者同一台电脑的另一个目录下克隆:
$ git clone git@github.com:maping/learngit.git
Cloning into 'learngit'...
remote: Counting objects: 44, done.
remote: Compressing objects: 100% (20/20), done.
remote: Total 44 (delta 22), reused 41 (delta 19), pack-reused 0
Receiving objects: 100% (44/44), done.
Resolving deltas: 100% (22/22), done.
Checking connectivity... done.
当你的小伙伴从远程库clone时,默认情况下,你的小伙伴只能看到本地的master分支。
可以用git branch命令查看:
$ git branch
* master
现在,你的小伙伴要在dev分支上开发,就必须创建远程origin的dev分支到本地,于是他用这个命令创建本地dev分支:
$ git checkout -b dev origin/dev
现在,他就可以在dev上继续修改,比如修改test.txt文件,然后,时不时地把dev分支push到远程:
$ vim test.txt
$ git add test.txt
$ git commit -m "add def in test.txt"
[dev 37eab3d] add def in test.txt
1 file changed, 1 insertion(+), 1 deletion(-)
$ git push origin dev
假设你的小伙伴已经向origin/dev分支推送了他的提交,而碰巧你也对test.txt文件的同一行作了修改,
$ vim test.txt
$ git add test.txt
$ git commit -m " add abcd in test.txt"
[dev 04e59fd] add abcd in test.txt
1 file changed, 1 insertion(+), 1 deletion(-)
并试图推送:
$ git push origin dev
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
To git@github.com:maping/learngit.git
! [rejected] dev -> dev (fetch first)
error: failed to push some refs to 'git@github.com:maping/learngit.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.
推送失败,因为你的小伙伴的最新提交和你试图推送的提交有冲突。
Git已经提示我们,先用git pull把最新的提交从origin/dev抓下来,然后,在本地合并,解决冲突,再推送:
$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:maping/learngit
3bbcb78..37eab3d dev -> origin/dev
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
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/ dev
git pull也失败了,原因是没有指定本地dev分支与远程origin/dev分支的链接,根据提示,设置dev和origin/dev的链接:
$ git branch --set-upstream dev origin/dev
The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
Branch dev set up to track remote branch dev from origin.
注意,这里提示 --set-upstream已经不推荐使用,建议使用--set-upstream-to。
再次git pull
$ git pull
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
这回git pull成功,但是合并有冲突,需要手动解决,解决的方法和分支管理中的解决冲突完全一样。
解决后,提交,
$ vim test.txt
$ git add test.txt
$ git commit -m "fix conflict of test.txt"
[dev 9f9a615] fix conflict of test.txt
再push:
$ git push origin dev
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 523 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
To git@github.com:maping/learngit.git
37eab3d..9f9a615 dev -> dev
4. 多人协作的工作模式通常是这样:
(1)首先,可以试图用git push origin branch-name推送自己的修改;
(2)如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
(3)如果合并有冲突,则解决冲突,并在本地提交;
(4)没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!
(5)如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name。
1. 查看远程库的信息
$ git remote
origin
或者用 git remote -v 显示更详细的信息
$ git remote -v
origin git@github.com:maping/learngit.git (fetch)
origin git@github.com:maping/learngit.git (push)
上面显示了可以抓取和推送的origin的地址。如果没有推送权限,就看不到push的地址。
2. 推送分支
推送分支就是把该分支上的所有本地提交推送到远程库。
推送时,要指定本地分支,这样,Git就会把该分支推送到远程库对应的远程分支上。
$ git push origin master
如果要推送其他分支,比如dev,就改成:
$ git push origin dev
那么,实际使用中,哪些分支需要推送呢?
master分支是主分支,因此要时刻与远程同步。
dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步。
bug分支只用于在本地修复bug,就没必要推到远程了。
feature分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发。
3. 抓取分支
多人协作时,大家都会往master和dev分支上推送各自的修改。
现在,模拟一个你的小伙伴,可以在另一台电脑(注意要把SSH Key添加到GitHub)或者同一台电脑的另一个目录下克隆:
$ git clone git@github.com:maping/learngit.git
Cloning into 'learngit'...
remote: Counting objects: 44, done.
remote: Compressing objects: 100% (20/20), done.
remote: Total 44 (delta 22), reused 41 (delta 19), pack-reused 0
Receiving objects: 100% (44/44), done.
Resolving deltas: 100% (22/22), done.
Checking connectivity... done.
当你的小伙伴从远程库clone时,默认情况下,你的小伙伴只能看到本地的master分支。
可以用git branch命令查看:
$ git branch
* master
现在,你的小伙伴要在dev分支上开发,就必须创建远程origin的dev分支到本地,于是他用这个命令创建本地dev分支:
$ git checkout -b dev origin/dev
现在,他就可以在dev上继续修改,比如修改test.txt文件,然后,时不时地把dev分支push到远程:
$ vim test.txt
$ git add test.txt
$ git commit -m "add def in test.txt"
[dev 37eab3d] add def in test.txt
1 file changed, 1 insertion(+), 1 deletion(-)
$ git push origin dev
假设你的小伙伴已经向origin/dev分支推送了他的提交,而碰巧你也对test.txt文件的同一行作了修改,
$ vim test.txt
$ git add test.txt
$ git commit -m " add abcd in test.txt"
[dev 04e59fd] add abcd in test.txt
1 file changed, 1 insertion(+), 1 deletion(-)
并试图推送:
$ git push origin dev
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
To git@github.com:maping/learngit.git
! [rejected] dev -> dev (fetch first)
error: failed to push some refs to 'git@github.com:maping/learngit.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.
推送失败,因为你的小伙伴的最新提交和你试图推送的提交有冲突。
Git已经提示我们,先用git pull把最新的提交从origin/dev抓下来,然后,在本地合并,解决冲突,再推送:
$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:maping/learngit
3bbcb78..37eab3d dev -> origin/dev
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
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/
git pull也失败了,原因是没有指定本地dev分支与远程origin/dev分支的链接,根据提示,设置dev和origin/dev的链接:
$ git branch --set-upstream dev origin/dev
The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
Branch dev set up to track remote branch dev from origin.
再次git pull
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
这回git pull成功,但是合并有冲突,需要手动解决,解决的方法和分支管理中的解决冲突完全一样。
$ git add test.txt
$ git commit -m "fix conflict of test.txt"
[dev 9f9a615] fix conflict of test.txt
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 523 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
To git@github.com:maping/learngit.git
37eab3d..9f9a615 dev -> dev
4. 多人协作的工作模式通常是这样:
(1)首先,可以试图用git push origin branch-name推送自己的修改;
(2)如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
(3)如果合并有冲突,则解决冲突,并在本地提交;
(4)没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!
(5)如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name。
没有评论:
发表评论