git中,對git push和push.default的認識

git push 命令

git push <遠程倉庫地址> <本地分支名>:<遠程分支名>  #  這裏的遠程倉庫地址可以用遠程倉庫別名來替代,比如origin。

關於orgin
知乎上的討論:https://www.zhihu.com/question/27712995
origin是用git克隆一個託管在Github上的代碼倉庫時,git默認創建的一個指向這個遠程代碼庫的標籤。(個人理解就像是C++中的一個宏定義)
查看遠程倉庫詳細信息(show remote url after name)
git remote -v

origin  https://github.com/TangShengqin/EasyFigure.git (fetch)
origin  https://github.com/TangShengqin/EasyFigure.git (push)

創建一個遠程倉庫名,和倉庫的url綁定
git remote add upstream https://github.com/TangShengqin/tsq.git

origin  https://github.com/TangShengqin/EasyFigure.git (fetch)
origin  https://github.com/TangShengqin/EasyFigure.git (push)
tsq     https://github.com/TangShengqin/tsq.git (fetch)
tsq     https://github.com/TangShengqin/tsq.git (push)

這樣,git fetch <name>,git fetch就可以直接用遠程倉庫的別名了。
git remote命令的官網說明鏈接:https://git-scm.com/docs/git-remote

git config中的關於push的配置:push.default

link: https://git-scm.com/docs/git-config

Defines the action git push should take if no refspec is explicitly given. Different values are well-suited for specific workflows; for instance, in a purely central workflow (i.e. the fetch source is equal to the push destination), upstream is probably what you want. Possible values are:

  • nothing - do not push anything (error out) unless a refspec is explicitly given. This is primarily meant for people who want to avoid mistakes by always being explicit.
    需要顯式的指出git push的參數,否則不能push,且會報錯。(直接git push會出錯)
  • current - push the current branch to update a branch with the same name on the receiving end. Works in both central and non-central workflows.
    推送當前的分支,來更新接收端(遠端)具有相同名字的分支。適用於中心化或者去中心化的workflow。如果遠程分支不存在相應的同名分支,則創建該分支,(不要求本地分支的倉庫和要推送的分支倉庫相同
  • upstream - push the current branch back to the branch whose changes are usually integrated into the current branch (which is called @{upstream}). This mode only makes sense if you are pushing to the same repository you would normally pull from (i.e. central workflow).
    推送當前分支到它的upstream(上游)分支上,這個模式只適用於推送的倉庫和git pull的倉庫相同。(不能將當前分支推送給另一個倉庫的上游分支)
    個人理解:自己有個倉庫repository1,遠端除了master分支,又新建了一個新分支branch1(但是branch1完全會繼承master分支,包括其後續的更改),本地我們更改了master分支,現在需要將master分支的更改推送到branch1上,就可以使用這種模式
  • tracking - This is a deprecated synonym for upstream. ## 啓用名詞,等效於upstream
  • simple - in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch’s name is different from the local one.

When pushing to a remote that is different from the remote you normally pull from, work as current. This is the safest option and is suited for beginners. This mode has become the default in Git 2.0.
在中央倉庫模式下,同upstream一樣,將本地的分支推送到上游分支,但是要求本地分支和上游分支的名字一樣,否則會拒絕推送。如果要推送到的遠程倉庫和git pull的倉庫不一致,處理方式同current一樣
這是最安全的模式,最適合新手,Git2.0之後的默認模式

  • matching - push all branches having the same name on both ends. This makes the repository you are pushing to remember the set of branches that will be pushed out (e.g. if you always push maint and master there and no other branches, the repository you push to will have these two branches, and your local maint and master will be pushed there).
    一次性的推送遠端和本地都存在的同名分支(如果目前只在一個分支上完成工作,其他分支還未完成,則這種方式不適合你,Git2.0之前的默認模式)
  1. 如果git push沒有指定where to push,即沒有指定要推送到哪個倉庫,則當前分支的branch.*.remote配置文件決定了需要推送到的倉庫。如果沒有這個配置文件,那麼默認倉庫就是originorigin也是倉庫創建時就建立的。
  2. git push命令行沒有指定要推送什麼分支,則命令會諮詢remote.*.push配置,如果其不存在,則會根據push.default配置文件來決定推送什麼(參考上面)。
  3. 當既沒有指定要推送的分支,也沒有指定要推送到哪個倉庫,則會按照push.defaultsimple模式來執行默認的行爲:當前分支被推送到相應的上游分支,但作爲安全措施,如果上游分支與本地分支的名稱不同,則中止推送。

設置 push.default

注意:push.default並不會在創建倉庫的時候被設置?
如果僅僅使用git push推送分支的時候,會出現以下警告信息

warning: push.default is unset; its implicit value has changed in Git 2.0 from 'matching' to 'simple'. 

輸入以下命令來設置push.default

git config --global push.default simple   # git2.0之後
git config --global push.default matching # git2.0之前
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章