1. 程式人生 > >git push和git pull的預設行為

git push和git pull的預設行為

一直以來對Git pushgit pull命令的預設行為感覺混亂,今天抽空總結下。

git push

通常對於一個本地的新建分支,例如git checkout -b develop, 在develop分支commit了程式碼之後,如果直接執行git push命令,develop分支將不會被push到遠端倉庫(但此時git push操作有可能會推送一些程式碼到遠端倉庫,這取決於我們本地git config配置中的push.default預設行為,下文將會逐一詳解)。

因此我們至少需要顯式指定將要推送的分支名,例如git push origin develop,才能將本地新分支推送到遠端倉庫。

當我們通過顯式指定分支名進行初次push操作後,本地有了新的commit,此時執行git push命令會有什麼效果呢?

如果你未曾改動過git config中的push.default屬性,根據我們使用的git不同版本(Git 2.0之前或之後),git push通常會有兩種截然不同的行為:

  1. develop分支中本地新增的commit被push到遠端倉庫
  2. push失敗,並收到git如下的警告
fatal: The current branch new has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin develop

為什麼git版本不同會有兩種不同的push行為?

因為在git的全域性配置中,有一個push.default屬性,其決定了git push操作的預設行為。在Git 2.0之前,這個屬性的預設被設為'matching',2.0之後則被更改為了'simple'。

我們可以通過git version確定當前的git版本(如果小於2.0,更新是個更好的選擇),通過git config --global push.default 'option'改變push.default的預設行為(或者也可直接編輯~/.gitconfig檔案)。

push.default 有以下幾個可選值:
nothing, current, upstream, simple, matching

其用途分別為:

  • nothing - push操作無效,除非顯式指定遠端分支,例如git push origin develop(我覺得。。。可以給那些不願學git的同事配上此項)。

  • current - push當前分支到遠端同名分支,如果遠端同名分支不存在則自動建立同名分支。

  • upstream - push當前分支到它的upstream分支上(這一項其實用於經常從本地分支push/pull到同一遠端倉庫的情景,這種模式叫做central workflow)。

  • simple - simple和upstream是相似的,只有一點不同,simple必須保證本地分支和它的遠端
    upstream分支同名,否則會拒絕push操作。

  • matching - push所有本地和遠端兩端都存在的同名分支。

因此如果我們使用了git2.0之前的版本,push.default = matching,git push後則會推送當前分支程式碼到遠端分支,而2.0之後,push.default = simple,如果沒有指定當前分支的upstream分支,就會收到上文的fatal提示。

upstream & downstream

git中存在upstream和downstream,簡言之,當我們把倉庫A中某分支x的程式碼push到倉庫B分支y,此時倉庫B的這個分支y就叫做A中x分支的upstream,而x則被稱作y的downstream,這是一個相對關係,每一個本地分支都相對地可以有一個遠端的upstream分支(注意這個upstream分支可以不同名,但通常我們都會使用同名分支作為upstream)。

初次提交本地分支,例如git push origin develop操作,並不會定義當前本地分支的upstream分支,我們可以通過git push --set-upstream origin develop,關聯本地develop分支的upstream分支,另一個更為簡潔的方式是初次push時,加入-u引數,例如git push -u origin develop,這個操作在push的同時會指定當前分支的upstream。

注意push.default = current可以在遠端同名分支不存在的情況下自動建立同名分支,有些時候這也是個極其方便的模式,比如初次push你可以直接輸入 git push 而不必顯示指定遠端分支。

git pull

弄清楚git push的預設行為後,再來看看git pull

當我們未指定當前分支的upstream時,通常git pull操作會得到如下的提示:

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> new1

git pull的預設行為和git push完全不同。當我們執行git pull的時候,實際上是做了git fetch + git merge操作,fetch操作將會更新本地倉庫的remote tracking,也就是refs/remotes中的程式碼,並不會對refs/heads中本地當前的程式碼造成影響。

當我們進行pull的第二個行為merge時,對git來說,如果我們沒有設定當前分支的upstream,它並不知道我們要合併哪個分支到當前分支,所以我們需要通過下面的程式碼指定當前分支的upstream:

git branch --set-upstream-to=origin/<branch> develop
// 或者git push --set-upstream origin develop 

實際上,如果我們沒有指定upstream,git在merge時會訪問git config中當前分支(develop)merge的預設配置,我們可以通過配置下面的內容指定某個分支的預設merge操作

[branch "develop"]
    remote = origin
    merge = refs/heads/develop // [1]為什麼不是refs/remotes/develop?

或者通過command-line直接設定:

git config branch.develop.merge refs/heads/develop

這樣當我們在develop分支git pull時,如果沒有指定upstream分支,git將根據我們的config檔案去merge origin/develop;如果指定了upstream分支,則會忽略config中的merge預設配置。

以上就是git push和git pull操作的全部預設行為,如有錯誤,歡迎斧正

[1] 為什麼merge = refs/heads/develop 而不是refs/remotes/develop?
因為這裡merge指代的是我們想要merge的遠端分支,是remote上的refs/heads/develop,文中即是origin上的refs/heads/develop,這和我們在本地直接執行git merge是不同的(本地執行git merge origin/develop則是直接merge refs/remotes/develop)。

相關推薦

git pushgit pull預設行為

一直以來對Git push與git pull命令的預設行為感覺混亂,今天抽空總結下。 git push 通常對於一個本地的新建分支,例如git checkout -b develop, 在develop分支commit了程式碼之後,如果直接執行git push命令,develop分支將不會被push到遠端

git fetch git pull 的差別

ria 版本 trac con etc 更新 _id track pull Git中從遠程的分支獲取最新的版本號到本地有這樣2個命令: 1. git fetch:相當於是從遠程獲取最新版本號到本地,不會自己主動merge git fetch or

git:Git fetchgit pull的區別, 解決Git報錯:error: You have not concluded your merge (MERGE_HEAD exists).

pre ret mas -h ruby error you origin 分支 Git fetch和git pull的區別, 解決Git報錯:error: You have not concluded your merge (MERGE_HEAD exists). 解決

Git fetchgit pull的區別

Git中從遠端的分支獲取最新的版本到本地有這樣2個命令:1. git fetch:相當於是從遠端獲取最新版本到本地,不會自動mergegit fetch origin mastergit log -p master..origin/mastergit merge origin/master    以上命令的含

git 檢視遠端倉庫的資訊 以及 git fetch git pull 的區別

1.檢視遠端倉庫的資訊 可以通過命令 git remote show [remote-name] 檢視某個遠端倉庫的詳細資訊,比如要看所克隆的 origin 倉庫,可以執行: git remote show origin 2.git fetch:相當

github上git clonegit push速度太慢的解決辦法

先到https://www.ipaddress.com/上查詢github.global.ssl.fastly.net最快的ip,然後在etc/hosts檔案下新增: 151.101.185.19

git fetch git pull 的差別

1、git fetch 相當於是從遠端獲取最新到本地,不會自動merge,如下指令:    git fetch or

利用git bashgit gui向git遠程倉庫提交文件

article bash 命令 rep 路徑 stage chang osi .html 1、首先在該文件夾下git init 2、然後在github下面創建一個新倉庫去存儲你的代碼 3、然後利用add添加遠程倉庫 4、然後點擊stage changed 5、最後點擊長傳

git merge git merge --no-ff

title ack lines rip print () pri addclass int 依據這張圖片能夠看出 git merge –no-ff 能夠保存你之前的分支歷史。能夠更好的查看 merge歷史。以及branch 狀態。 git m

git addgit commit

stage mod com 指定 for 命令 現在 ssa -m git命令使用:提交前可指定要提交哪些文件,然後使用git commit來提交 樣例: git status 輸出: Changes to be committed: modified: ap

git 命令 git stash git stash pop

今天 merge 報錯 版本 pull 一個 一次 後來 更新 今天好心累 居然把我寫的東西都沒了 本地和遠程的版本差別太大 於是我想更新一下 更新的時候報錯了 於是我按照老師的來 之前忘記了他怎麽操作的 只記得有 git stash -> git merge -&

使用plumbing命令來深入理解git addgit commit的工作原理

clean 結果 write 文件的 repos 倉庫 head 根據 acc 前言: plumbing命令 和 porcelain命令 git中的命令分為plumbing命令和porcelain命令: porcelain命令就是我們常用的git add,git comm

伺服器上安裝git倉庫git本地

 原文來自:    https://blog.csdn.net/li_wen01/article/details/52411543 git伺服器搭建,本來是一件簡單的事情,但是因為網上的很多教程都不詳細,造成的後果就是搭建出來的伺服器很多的許可權的問題,於

git的常用指令(二) git add -A 、git add . git add -u

git add . :他會監控工作區的狀態樹,使用它會把工作時的所有變化提交到暫存區,包括檔案內容修改(modified)以及新檔案(new),但不包括被刪除的檔案。 git add -u :他僅監控已經被add的檔案(即tracked file),他會將被修改的檔案提交到暫存區。add -u 不會提交新檔

總結-DOM通用屬性&DOM獲取元素位置&事件繫結與移除&冒泡標籤的預設行為

DOM通用屬性&DOM獲取元素位置&事件繫結與移除&冒泡和標籤的預設行為 DOM物件的通用屬性 innerHTML 獲取/設定元素裡的html內容。在設定元素的HTML內容的時候,會覆蓋掉原來的內容。 innerText 獲取/設定元素裡面的文字

ADB push ADB pull命令

分享圖片 pull 上傳 strong img height adb push 9.png ima adb push命令 :從電腦上傳送文件到手機; adb pull命令 :從手機傳送文件到電腦上 ADB push 和ADB pull命

git rebase git merge 的區別

git rebase 和 git merge 的區別  AlvinStar 關注 2016.07.31 17:32* 字數 760 閱讀 14895評論 6喜歡 28讚賞 1 Description git rebase 和 git merge

jq 阻止冒泡事件瀏覽器的預設行為

<a href="http://www.baidu.com" id="link">百度</a><script src="jquery-1.12.4.js"></script><script> $(function () { $("#

git revertgit reset的區別

原文:http://blog.csdn.net/koffuxu/article/details/6731876 git revert  是生成一個新的提交來撤銷某次提交,此次提交之前的commit都會被保留 git reset  是

git init git --bare init的區別

git init  和 git --bare init 的區別在於: git init 帶工作區 git --bare init  是不帶工作區的,只有版本庫,想要看git裡面的內容只能通過git clone address的方式克隆出來 而我們在初始化遠端倉庫時最好使