Git 的origin和master剖析
http://lishicongli.blog.163.c...
首先要明确一点,对git的操作是围绕3个大的步骤来开展的(其实简直所有的SCM都是这样)
- 从git取数据(git clone)
- 改变代码
- 将改变传回git(git push)
这3个步骤又波及到两个repository,一个是remote repository,再近程服务器上,一个是local repository,再本人工作区上。其中
1, 3两个步骤波及到remote server/remote repository/remote branch,
2波及到local repository/local branch。git clone 会依据你指定的remote server/repository/branch,拷贝一个正本到你本地,再git push之前,你对所有文件的改变都是在你本人本地的local repository来做的,你的改变(local branch)和remote branch是独立(并行)的。Gitk显示的就是local repository。
在clone实现之后,Git 会主动为你将此近程仓库命名为origin(origin只相当于一个别名,运行git remote –v或者查看.git/config能够看到origin的含意),并下载其中所有的数据,建设一个指向它的master 分支的指针,咱们用(近程仓库名)/(分支名) 这样的模式示意近程分支,所以origin/master指向的是一个remote branch(从那个branch咱们clone数据到本地),但你无奈在本地更改其数据。
同时,Git 会建设一个属于你本人的本地master 分支,它指向的是你刚刚从remote server传到你本地的正本。随着你一直的改变文件,git add, git commit,master的指向会主动挪动,你也能够通过merge(fast forward)来挪动master的指向。
$git branch -a (to show all the branches git knows about)
- master
remotes/origin/HEAD -> origin/master
remotes/origin/master
$git branch -r (to show remote branches git knows about)
origin/HEAD -> origin/master
origin/master
能够发现,master就是local branch,origin/master是remote branch(master is a branch in the local repository. remotes/origin/master is a branch named master on the remote named origin)
$git diff origin/master master (show me the changes between the remote master branch and my master branch).
须要留神的是,remotes/origin/master和origin/master的指向是雷同的
$git diff origin/master remotes/origin/master
git push origin master
origin指定了你要push到哪个remote
master其实是一个“refspec”,失常的“refspec”的模式为”+<src>:<dst>”,冒号前示意local branch的名字,冒号后示意remote repository下 branch的名字。留神,如果你省略了<dst>,git就认为你想push到remote repository下和local branch雷同名字的branch。听起来有点拗口,再解释下,push是怎么个push法,就是把本地branch指向的commit push到remote repository下的branch,比方
$git push origin master:master (在local repository中找到名字为master的branch,应用它去更新remote repository下名字为master的branch,如果remote repository下不存在名字是master的branch,那么新建一个)
$git push origin master (省略了<dst>,等价于“git push origin master:master”)
$git push origin master:refs/for/mybranch (在local repository中找到名字为master的branch,用他去更新remote repository上面名字为mybranch的branch)
$git push origin HEAD:refs/for/mybranch (HEAD指向当前工作的branch,master不肯定指向当前工作的branch,所以我感觉用HEAD还比master好些)
$git push origin :mybranch (再origin repository外面查找mybranch,删除它。用一个空的去更新它,就相当于删除了)