Git 的origin和master解析
首先要明確一點(diǎn),對(duì)git的操作是圍繞3個(gè)大的步驟來(lái)展開的(其實(shí)幾乎所有的SCM都是這樣)
1.?????從git取數(shù)據(jù)(git clone)
2.?????改動(dòng)代碼
3.?????將改動(dòng)傳回git(git push)
這3個(gè)步驟又涉及到兩個(gè)repository,一個(gè)是remote repository,再遠(yuǎn)程服務(wù)器上,一個(gè)是local repository,再自己工作區(qū)上。其中
1, 3兩個(gè)步驟涉及到remote server/remote repository/remote branch,
2涉及到local repository/local branch。git clone?會(huì)根據(jù)你指定的remote server/repository/branch,拷貝一個(gè)副本到你本地,再git push之前,你對(duì)所有文件的改動(dòng)都是在你自己本地的local repository來(lái)做的,你的改動(dòng)(local branch)和remote branch是獨(dú)立(并行)的。Gitk顯示的就是local repository。
?
在clone完成之后,Git?會(huì)自動(dòng)為你將此遠(yuǎn)程倉(cāng)庫(kù)命名為origin(origin只相當(dāng)于一個(gè)別名,運(yùn)行git remote –v或者查看.git/config可以看到origin的含義)(guyue: 即origin是代表遠(yuǎn)程倉(cāng)庫(kù)的一個(gè)默認(rèn)別名, 可以在項(xiàng)目工程目錄中.git/config文件中查看或修改),并下載其中所有的數(shù)據(jù),建立一個(gè)指向它的master?分支的指針,我們用(遠(yuǎn)程倉(cāng)庫(kù)名)/(分支名)?這樣的形式表示遠(yuǎn)程分支,所以origin/master指向的是一個(gè)remote branch(從那個(gè)branch我們clone數(shù)據(jù)到本地),但你無(wú)法在本地更改其數(shù)據(jù)。
同時(shí),Git?會(huì)建立一個(gè)屬于你自己的本地master?分支,它指向的是你剛剛從remote server傳到你本地的副本。隨著你不斷的改動(dòng)文件,git add, git commit,master的指向會(huì)自動(dòng)移動(dòng),你也可以通過(guò)merge(fast forward)來(lái)移動(dòng)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
?
可以發(fā)現(xiàn),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到哪個(gè)remote
master其實(shí)是一個(gè)“refspec”,正常的“refspec”的形式為”+<src>:<dst>”,冒號(hào)前表示local branch的名字,冒號(hào)后表示remote repository下?branch的名字。注意,如果你省略了<dst>,git就認(rèn)為你想push到remote repository下和local branch相同名字的branch。聽起來(lái)有點(diǎn)拗口,再解釋下,push是怎么個(gè)push法,就是把本地branch指向的commit push到remote repository下的branch,比如
$git push origin master:master?(在local repository中找到名字為master的branch,使用它去更新remote repository下名字為master的branch,如果remote repository下不存在名字是master的branch,那么新建一個(gè))
$git push origin master?(省略了<dst>,等價(jià)于“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指向當(dāng)前工作的branch,master不一定指向當(dāng)前工作的branch,所以我覺得用HEAD還比master好些)
$git push origin :mybranch?(再origin repository里面查找mybranch,刪除它。用一個(gè)空的去更新它,就相當(dāng)于刪除了)
總結(jié)
以上是生活随笔為你收集整理的Git 的origin和master解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: vim 安装vim-prettier
- 下一篇: 跨域与跨域访问