2017-09-12 42 views
0

我有一個Bash函數來防止推送到沒有上游分支的遠程存儲庫(例如GitHub)時的錯誤。也許你熟悉這個錯誤:切換到git config push.default current有什麼影響?

$ git checkout -b test 
$ git push 
fatal: The current branch test has no upstream branch. 
To push the current branch and set the remote as upstream, use 

    git push --set-upstream origin test 

爲了解決這個問題,我做的是攔截到git push任何電話,看看我得到與沒有上游分支的錯誤。在出現這種錯誤的情況下,我的終端會自動爲我執行它,而不是複製並粘貼上面的建議命令。

這是代碼,如果你是好奇: https://github.com/arturoherrero/dotfiles/blob/6f517a0b7287ac61174dfd2b6c9ee5bf9a9c2e96/system/git.sh#L22-L34


我現在的Git的配置是push.default simple,但今天我剛剛意識到,我可以使用push.default current來實現相同的行爲(除我的自定義代碼)。

Ref。 https://www.kernel.org/pub/software/scm/git/docs/git-config.html

  • 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.

  • 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.

那麼,什麼是切換到git config push.default current影響?我想了解一些可能出現問題的情況,因爲Git具有不同的行爲。

回答

2

其影響是,當你推到任何回購,git會假設分支名稱相應。如果你只是推送到一個單一的遠程(origin),並且你總是對遠程和本地的同一分支使用相同的名稱,那麼它會沒事的。如果你想設置其他類型的分支映射 - 例如,如果本地功能分支feature_1應該去遠程分支dev/features/feature_1或類似的東西 - 那麼你不想使用simple作爲你的push.default

這裏沒有任何隱藏的含義;您發佈的文檔解釋了行爲,如果這是您想要的行爲,您可以使用它。

相關問題