大部分人都会使用git的默认远程分支和本地分支的对应关系,但是git提供的机制比默认的mapping关系要复杂一些。refspec就是我们的本地分支和远程分支的mapping关系描述。



fetch的 refspec:


如果你的git仓库是从github clone下来的: 

git remote add origin https://github.com/schacon/simplegit-progit

如果你打开你的 .git/config文件,找到origin这个remote,可以看到默认的origin的fetch的时候的refspec:

[remote "origin"]
	url = https://github.com/schacon/simplegit-progit
	fetch = +refs/heads/*:refs/remotes/origin/*
CODE



格式: +<src>:<dst>  src是远端的ref的模式, <dst>是本地的模式, +的意思是即使不是fast-forward的, 也需要更新(会产生merge)

默认的refspec在git remote add origin的时候回自动加上,远端的分支(在远端仓库里面的 refs/heads/)都被同步到本地的 refs/remotes/origin/


如果你每次pull的时候只需要pull master分支,可以: 

fetch = +refs/heads/master:refs/remotes/origin/master
CODE


push的refspec


如果想把本地分支push到不同的远端分支,可以用git命令:

git push origin master:refs/heads/qa/master

如果想自动化,则需要加push的refspec:


[remote "origin"]
	url = https://github.com/schacon/simplegit-progit
	fetch = +refs/heads/*:refs/remotes/origin/*
	push = refs/heads/master:refs/heads/qa/master
CODE