簡體   English   中英

如何配置 git push 以在沒有 -u 的情況下自動設置上游?

[英]How to configure git push to automatically set upstream without -u?

我希望git push origin在我第一次推送本地創建的分支時自動設置上游引用。

我知道git push -u ,但我不想考慮我之前是否使用過-u或以其他方式設置上游參考。 換句話說,我希望git push自動對沒有上游的分支的任何推送產生git push -u的效果。

這可能嗎? 如果它需要別名或實用程序腳本,那很好。

您可以使用git config --global push.default current ( docs ) 對其進行配置,以使其推送當前分支以更新具有相同名稱的分支。

2022 年更新 (git>=2.37.0)

git config --global --add --bool push.autoSetupRemote true實現了同樣的效果,同時還設置了上游跟蹤( docs )。

由於我認為使用 git config 不可能做到這一點,因此您可以在 bash 中執行以下操作:

[[ $(git config "branch.$(git rev-parse --abbrev-ref HEAD).merge") = '' ]] && git push -u || git push

如果當前分支有遠程跟蹤分支,則調用git push否則調用git push -u

2022: Git 2.37 提出

git config --global push.autoSetupRemote true

push.autoSetupRemote

如果設置為“ true ”,當當前分支不存在上游跟蹤時,默認推送時假定--set-upstream

此選項與push.default選項“ simple ”、“ upstream ”和“ current ”一起生效。

如果默認情況下您希望將新分支推送到默認遠程(如“ push.default=current ”的行為)並且您還希望設置上游跟蹤,這很有用。
最有可能從該選項中受益的工作流程是“ simple的”中央工作流程,其中所有分支都應在遠程具有相同的名稱。


2013:注意:新的默認推送策略“ simple依賴於具有上游分支的分支這一事實意味着:
設置上游分支被視為自願步驟,而不是隱藏的自動化步驟

當“ git push [$there] ”沒有說明要推送什么時,到目前為止,我們一直使用傳統的“匹配”語義(只要那里已經有同名的分支,您的所有分支都被發送到遠程) .

我們將使用“ simple ”語義將當前分支推送到同名分支,僅當當前分支設置為與該遠程分支集成時
有一個用戶偏好配置變量“ push.default ”來改變它。


因此,根據mechanicfish答案構建,您可以定義一個別名,右雙引號( " )轉義( \" ):

git config alias.pu "![[ $(git config \"branch.$(git rev-parse --abbrev-ref HEAD).merge\") = '' ]] && git push -u || git push"

git pu origin

Sc0ttyD 在評論中提出以下別名:

alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push'

在多行中:

alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] && 
           git push -u origin $(git symbolic-ref --short HEAD) || 
           git push'

我有同樣的問題。 我已經制作了這個別名(來自我的.gitconfig

[alias]
    track = "!git branch --set-upstream-to=origin/`git symbolic-ref --short HEAD`"

用法:

  1. 每個新分支一次(當前已簽出): git track
  2. 正常推送:)

@VonC 和 @Frexuz 的答案很有幫助,但他們的兩個解決方案都對我產生了錯誤。 使用他們的兩個答案,我拼湊了一些對我有用的東西:

    [alias]
    pu = ![[ $(git config "branch.$(git symbolic-ref --short HEAD).merge") = '' ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push

這將導致執行git push -u origin $BRANCHNAMEgit push ,具體取決於其上游(屬性branch.$BRANCHNAME.merge )是否已定義。

在命令行輸入此別名需要轉義碼,因此使用編輯器插入正確的文件( $HOME/.gitconfig (全局)、. .git/config (本地)或/etc/gitconfig (系統) )

簡短的回答

如果您確實喜歡明確並在必要時使用-u選項,但不想輸入整個:

git push -u origin foo

然后您可以使用以下別名:

[alias]
    push-u = !git push -u origin $(git symbolic-ref --short HEAD)

只需鍵入:

git push-u

長答案

通常,當我們剛剛創建了一個新的本地分支並提交,並且我們想將其推送到上游時,需要-u--set-upstream的簡寫)。 遠程倉庫還沒有新的分支,所以我們需要在推送提交之前告訴 git 創建並跟蹤遠程分支。 這僅對分支上的第一次推送是必需的。 這是一個典型的場景:

git checkout -b foo         # Create local branch
git commit -m "Foo"         # Create local commit
git push -u origin foo      # Create and track remote branch, and push commit
git commit -m "Bar"         # Create local commit
git push                    # Push commit

就個人而言,我確實喜歡在創建遠程分支時明確使用git push -u :這是一個非常重要的操作,與世界共享一個全新的分支。

但是,我討厭我們必須明確編寫git push -u origin foo 不僅打字很痛苦,更重要的是,它很容易出錯! 輸入分支名稱很容易出錯,新的遠程分支不會和你的本地分支同名! 在大多數情況下,實際上,您希望上游存儲庫是origin ,並且上游分支與您的本地分支具有相同的名稱。

因此,我在我的.gitconfig中使用了以下別名,這是Mark 提供的優秀答案的子集:

[alias]
    push-u = !git push -u origin $(git symbolic-ref --short HEAD)

現在,我們可以執行以下操作,這仍然是明確的,但不太容易出錯:

git checkout -b foo         # Create local branch
git commit -m "Foo"         # Create local commit
git push-u                  # Create and track remote branch, and push commit
git commit -m "Bar"         # Create local commit
git push                    # Push commit

我通過使用這個簡單的 Bash 腳本解決了這個問題。 它不適用於現有分支,但如果您使用此功能創建所有分支,您將始終自動設置上游分支。

function con { git checkout -b $1 && git push --set-upstream origin $1; }

$1 代表你在con之后傳遞的第一個參數,所以就像這樣做:

git checkout -b my-new-branch && git push -u my-new-branch

...通過這樣做:

con my-new-branch

如果您只想通過不太可能的按鍵來使用內置的 git 功能,只需鍵入:

$ git push -uo選項卡H選項卡

並且自動完成會給你$ git push -u origin HEAD

要在 OSX 上啟用自動編譯,請使用此內容設置一個~/.git-completition.bash文件,並將以下行添加到您的~/.bash_profile文件中並重新啟動您的終端:

# git branch autocomplete
if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

它也會影響內置終端,例如 vscode 中的終端等。

對此唯一完全誠實的答案是“你不能”。

我已經閱讀了本文中的所有回復,以及其他提出相同問題的問題。

發布的每個答案仍然需要您在第一次推送到新分支時傳遞特殊參數。

簡單地:

$ alias gush="git push -u origin HEAD"

今天我遇到了(一個新的?)選項“push.autoSetupRemote”。 git config help 說:

If set to "true" assume --set-upstream on default push when no upstream tracking exists for the current branch; this option takes effect with push.default options simple,
       upstream, and current. It is useful if by default you want new branches to be pushed to the default remote (like the behavior of push.default=current) and you also want
       the upstream tracking to be set. Workflows most likely to benefit from this option are simple central workflows where all branches are expected to have the same name on
       the remote.

我用有用的腳本做了一個 git 擴展,包括這個:

usage: git line push

Push the current branch and set an upstream if needed.

https://github.com/jvenezia/git-line

如果無論出於何種原因,其他答案都不適合您,那么您可以用這個 bash 函數替換git push ,以便在必要時自動重新發送帶有正確標志的推送請求。

gitpush()
{
    git push -v 2>&1 | # perform push command, pipe all output
    tee /dev/tty | # keep output on screen and pipe it forward
    (
     cmd=$(sed -n "s/^.*\(git push --set-upstream origin .*\)$/\1/p");
     [[ -n "${cmd// }" ]] && (echo "> $cmd"; eval $cmd);
    ) # if we get output that matches the command to perform, execute it
}

您將犧牲推送輸出的進度部分,但除此之外,一切都按預期工作。

就個人而言,我將使用JT Jobe 的答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM