目錄:
git 實踐(一) pull的使用
git 實踐(二) push的使用
git 實踐(三) 建立關(guān)聯(lián)分支
前言
工作中,我們會用到git pull來從遠(yuǎn)程倉庫"同步"代碼吗蚌,通常有三種方式;
git pull origin <remote_branch>:<local_branch>
git pull origin <remote_branch>
git pull
這三種用法充分詮釋了什么是簡即繁,繁即簡香嗓;看上去簡單的驶拱,往往背后蘊藏玄機额获;
測試環(huán)境:
本地分支:master和dev
遠(yuǎn)程分支:master和dev
$ git branch -a
* dev
master
remotes/origin/HEAD -> origin/master
remotes/origin/dev
remotes/origin/master
1.git pull origin <remote_branch>:<local_branch>
這種用法寫起來最為繁瑣够庙,但最好理解:
場景:當(dāng)本地的當(dāng)前分支不是local_branch;
作用:將遠(yuǎn)程分支拉取到指定本地分支抄邀;
例如:當(dāng)前分支是dev耘眨,但是你想把遠(yuǎn)程master”同步”到本地master,但又不想使checkout切換到master分支撤摸;
這時你就可以使用git pull origin master:master
zhangchangzhi@ZBXXXX /e/02.Workspace-test/gitTest (dev)
$ git pull origin master:master
From https://github.com/jinxintang/gitTest
a09fdc4..941758f master -> master
Already up-to-date.
從上述代碼可以看到毅桃,我當(dāng)前分支為dev,但執(zhí)行"同步”操作的卻是在master分支;
2.git pull origin <remote_branch>
有了上面的例子准夷,這種使用方法的場景和作用就好理解了:
場景:在當(dāng)前分支上進(jìn)行同步操作;
作用:將指定遠(yuǎn)程分支同步到當(dāng)前本地分支莺掠;
廢話不說衫嵌,上代碼:
zhangchangzhi@ZBXXX /e/02.Workspace-test/gitTest (dev)
$ git pull origin master
From https://github.com/jinxintang/gitTest
* branch master -> FETCH_HEAD
Already up-to-date.
把遠(yuǎn)程master分支同步到HEAD分支(HEAD分支指向當(dāng)前位置);
3.git pull
這種寫法最簡單彻秆,也最常用楔绞,但是隱含的知識也是最多的;
場景:本地分支已經(jīng)和想要拉取的分支建立了“關(guān)聯(lián)”關(guān)系唇兑;
作用:拉取所有遠(yuǎn)程分支的新版本"坐標(biāo)"酒朵,并同步當(dāng)前分支的本地代碼(具體根據(jù)關(guān)聯(lián)分支而定)
什么是"關(guān)聯(lián)"分支?
首先我們先使用git branch -vv
查看一下目前分支的“關(guān)聯(lián)”情況;
$ git branch -vv
* dev 1a1b215 [origin/dev] Merge branch 'master' of https://github.com/jinxintang/gitTest into dev
master a09fdc4 [origin/master] create pull
可以看到我們的本地的dev關(guān)聯(lián)的是遠(yuǎn)程(origin)的dev扎附,本地的master關(guān)聯(lián)的是遠(yuǎn)程(origin)的master;
那么這種關(guān)聯(lián)是如何建立蔫耽、是否可以修改呢;
配置本地分支與遠(yuǎn)程分支的三種方法:
1.檢出時建立關(guān)聯(lián)關(guān)系:git checkout -b dev origin/dev
當(dāng)我們檢查時留夜,git會自動為我們檢出的分支和遠(yuǎn)程分支建立關(guān)聯(lián)關(guān)系匙铡;
2.提交時配置關(guān)聯(lián)關(guān)系:git push -u origin <remote_branch>
或git push --set-upstream origin <remote_branch>
zhangchangzhi@ZB-PF0SB6DQ MINGW64 /e/02.Workspace-test/gitTest (dev_zcz)
$ git branch -vv
* dev_zcz 3b7001a [origin/dev] cm
master a09fdc4 [origin/master] create pull
zhangchangzhi@ZB-PF0SB6DQ MINGW64 /e/02.Workspace-test/gitTest (dev_zcz)
$ git push -u origin dev_zcz
Everything up-to-date
Branch dev_zcz set up to track remote branch dev_zcz from origin.
zhangchangzhi@ZB-PF0SB6DQ MINGW64 /e/02.Workspace-test/gitTest (dev_zcz)
$ git branch -vv
* dev_zcz 3b7001a [origin/dev_zcz] cm
master a09fdc4 [origin/master] create pull
通過上面的例子可以看到push前dev_zcz關(guān)聯(lián)的是origin/dev,執(zhí)行push -u 后管理分支改為origin/dev_zcz
注:默認(rèn)配置下图甜,提交時本地分支需和遠(yuǎn)程分支同名;
3.更改git/config文件:git branch --set-upstream-to=<remote_branch>
zhangchangzhi@ZB-PF0SB6DQ MINGW64 /e/02.Workspace-test/gitTest (dev_zcz)
$ git branch --set-upstream-to=origin/zcz
Branch dev_zcz set up to track local branch origin/zcz.
zhangchangzhi@ZB-PF0SB6DQ MINGW64 /e/02.Workspace-test/gitTest (dev_zcz)
$ git branch -vv
* dev_zcz 3b7001a [origin/zcz] cm
master a09fdc4 [origin/master] create pull
origin/zcz 3b7001a [dev_zcz] cm
無論使用上述那種方法鳖眼,本地分支和遠(yuǎn)程分支的“關(guān)聯(lián)”最終都會寫到config文件黑毅;
zhangchangzhi@ZB-PF0SB6DQ MINGW64 /e/02.Workspace-test/gitTest/.git (GIT_DIR!)
$ cat config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = https://github.com/jinxintang/gitTest.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "dev_zcz"]
remote = .
merge = refs/heads/origin/zcz
[branch "origin/zcz"]
remote = .
merge = refs/heads/dev_zcz
注:本項目的配置信息存放目錄:項目所在目錄/.git/config
看完這三種配置關(guān)聯(lián)分支的方法,想必大家已經(jīng)對“關(guān)聯(lián)分支”有了一定了解钦讳;
關(guān)聯(lián)分支:在git中表現(xiàn)為upstream,無論是使用push -u 或是 git branch --set-upstream-to方法矿瘦,均會將這種對應(yīng)關(guān)系寫入.git/config配置文件,如果一個本地分支沒有關(guān)聯(lián)分支愿卒,則無法執(zhí)行 git push 或 git pull指令匪凡;
沒有"關(guān)聯(lián)"分支的情況下,使用push會先讓你設(shè)置一個upstream branch.
zhangchangzhi@ZB-PF0SB6DQ MINGW64 /e/02.Workspace-test/gitTest (dev_no_upstream)
$ git branch -vv
* dev_no_upstream 3b7001a cm
dev_zcz 3b7001a [origin/zcz] cm
master a09fdc4 [origin/master] create pull
origin/zcz 3b7001a [dev_zcz] cm
zhangchangzhi@ZB-PF0SB6DQ MINGW64 /e/02.Workspace-test/gitTest (dev_no_upstream)
$ git push
fatal: The current branch dev_no_upstream has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin dev_no_upstream
那么建立了一個關(guān)聯(lián)分支掘猿,是否就一定能使用git push呢病游?請閱讀<git 實踐(二)push的使用>