推送和抓取分支
我们在本地仓库上有 master
分支,但是如何将本地分支推送到远程分支呢,使用命令git push
即可:
例如,现在我们的本地仓库中有两个分支:master
和 dev
, 现在将两个分支推送到远程仓库(github)上:git push origin <local branch name>
$ git push origin master
$ git push origin dev
注意:`origin` 是默认的远程仓库名字,后面的分支名字是本地仓库中分支的名字,
这样,Git就会把本地分支推送到远程仓库对应的分支上(远程仓库没有,则
Git会默认创建一个)
此外,并不是所有的分支都需要推送到远程仓,分支是否需要推送到远程仓,视自己的情况需要而定。
抓取分支:
你可以往远程仓推送自己的分支,同时也可以从克隆远程仓到本地(相当于下载),现在,我们在另外一台电脑上克隆仓库 firstRep
,使用命令git clone <repository name>
:,现在我在windows上使用Git bash
模仿你的一位同事:
$ git clone git@github.com:xxxxx/firstRep.git
Cloning into 'firstRep'...
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 16 (delta 3), reused 16 (delta 3), pack-reused 0
Receiving objects: 100% (16/16), done.
Resolving deltas: 100% (3/3), done.
然后我们进入到仓库,然后再来查看克隆到本地的分支情况,使用命令git branch
:
$ cd firstRep
$ git branch
* master
ok,发现远程仓中的 dev
分支并不存在,其实在默认情况下,你的小伙伴使用git clone
命令只能看到本地的 master
分支。
现在你的小伙伴想要在 dev
分支上开发,就必须创建远程 origin
的 dev
分支到本地,使用下面的命令即可创建远程分支到本地:
$ git checkout -b dev origin/dev
Switched to a new branch 'dev'
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
看到Git此时告诉我们,我们可以使用这个本地分支 dev
分支来追踪远程库中的 dev
分支。
现在他就可以在这个dev
分支上工作,并且推送到远程的 dev
分支。
例如:我们新创建一个 a.txt
文件添加并提交至仓库中:
$ cat a.txt
test dev
$ git add readme.txt
$ git commit -m 'commit a.txt'
$ git commit -m 'commit a.txt'
[dev ca21716] commit a.txt
1 file changed, 1 insertion(+)
create mode 100644 a.txt
然后可以推送到远程库中的 dev
分支。
$ git push origin dev
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 276.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:xxxxxx/firstRep.git
a688f95..ca21716 dev -> dev
现在我们在远程仓中的 dev
分支看到的最新的提交是你的小伙伴提交内容。一切都是那么优雅,如丝般顺滑,但是到你这就可能会出错了,继续往下看。
碰巧你也新建了一个文件名一样的文件 a.txt
,添加了一些内容,然后想要推送至远程仓的 dev
分支:
$ cat a.txt
$ git add a.txt
$ git commit -m 'commit this txt'
[master 0ac9d23] commit this txt
1 file changed, 1 insertion(+)
create mode 100644 a.txt
然后试图推送到远程仓:
$ git push origin dev
To github.com:xxx/firstRep.git
! [rejected] dev -> dev (fetch first)
error: failed to push some refs to '[email protected]:xxx/firstRep.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提示我们可以使用 git pull
抓取分支然后再推送:所以我们试着使用它提示的方法来抓取分支:
$ git pull origin dev
From github.com:xxxx/firstRep
* branch dev -> FETCH_HEAD
Auto-merging a.txt
CONFLICT (add/add): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
可以看到出现冲突,于是我们可以解决冲突:git status
告诉我们冲突的文件:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both added: a.txt
no changes added to commit (use "git add" and/or "git commit -a")
可以看到存在冲突:查看 a.txt
文件的内容:
$ cat a.txt
<<<<<<< HEAD
test pull
=======
test dev
>>>>>>> ca217161af9b857fdede6699404ad322a0bb17b2
可以看到Git使用不同的符号:<<<<<<<
、=======
、>>>>>>>
,来标记出当前版本的a.txt
和 你的某个小伙伴提交的版本的 a.txt
的不同内容。
我们必须手动解决冲突:
//手动更改为:test pull and dev
$ cat a.txt
test pull and dev
然后再提交修改然后推送:
$ git push
Counting objects: 2, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 337 bytes | 168.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:CoderCXF/firstRep.git
c869bd5..fc6a2fd dev -> dev
推送成功。在github 上也可以看到文件 a.txt
合并后的内容。
$ cat a.txt
test pull and dev
ok, 至此所有的问题都已经成功解决。
小结:
1、推送分支至远程仓:git push origin <branch name>
,如果推送失败的话,先使用git pull
抓取分支,然后合并内容;
2、本地新建的分支如果不推送到远程,对其他人就是不可见的;
3、如果需要自己关联到别人创建的另一个分支,使用命令:
git checkout -b <branch name> origin/<branch name>
,最好自己本地创建的分支和远程分支名称一样;
4、建立远程分支和本地分支的关联:使用命令:
git branch --set-upstream branch-name origin/branch-name
参考:
【1】廖雪峰:Git教程