Git 的origin和master分析

首先要明確一點,對git的操做是圍繞3個大的步驟來展開的(其實幾乎全部的SCM都是這樣) git

1.     git取數據(git clone 服務器

2.     改動代碼 spa

3.     將改動傳回gitgit push 指針

3個步驟又涉及到兩個repository,一個是remote repository,再遠程服務器上,一個是local repository,再本身工做區上。其中 server

1, 3兩個步驟涉及到remote server/remote repository/remote branch rem

2涉及到local repository/local branchgit clone 會根據你指定的remote server/repository/branch,拷貝一個副本到你本地,再git push以前,你對全部文件的改動都是在你本身本地的local repository來作的,你的改動(local branch)remote branch是獨立(並行)的。Gitk顯示的就是local repository it

 

clone完成以後,Git 會自動爲你將此遠程倉庫命名爲originorigin只至關於一個別名,運行git remote –v或者查看.git/config能夠看到origin的含義),並下載其中全部的數據,創建一個指向它的master 分支的指針,咱們用(遠程倉庫名)/(分支名) 這樣的形式表示遠程分支,因此origin/master指向的是一個remote branch(從那個branch咱們clone數據到本地),但你沒法在本地更改其數據。 ast

同時,Git 會創建一個屬於你本身的本地master 分支,它指向的是你剛剛從remote server傳到你本地的副本。隨着你不斷的改動文件,git add, git commitmaster的指向會自動移動,你也能夠經過mergefast 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 branchorigin/masterremote branchmaster 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/masterorigin/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就認爲你想pushremote repository下和local branch相同名字的branch。聽起來有點拗口,再解釋下,push是怎麼個push法,就是把本地branch指向的commit pushremote repository下的branch,好比

$git push origin master:master (local repository中找到名字爲masterbranch,使用它去更新remote repository下名字爲masterbranch,若是remote repository下不存在名字是masterbranch,那麼新建一個)

$git push origin master (省略了<dst>,等價於「git push origin master:master」)

$git push origin master:refs/for/mybranch (local repository中找到名字爲masterbranch,用他去更新remote repository下面名字爲mybranchbranch)

$git push origin HEAD:refs/for/mybranch HEAD指向當前工做的branchmaster不必定指向當前工做的branch,因此我以爲用HEAD還比master好些)

$git push origin :mybranch (再origin repository裏面查找mybranch,刪除它。用一個空的去更新它,就至關於刪除了)

相關文章
相關標籤/搜索