【问题标题】:git config default push destination for new branchesgit config 新分支的默认推送目的地
【发布时间】:2018-10-06 18:33:41
【问题描述】:

如果您通过以下方式创建本地存储库:

git init

那么如何为git push 设置默认远程,这样如果您创建一个新分支并希望git push 它的提交就不需要指定远程目标?

考虑到本地存储库没有从远程克隆,只是初始化,因此它没有任何要推送的提交或分支。

到目前为止,我只设法为 master 设置了远程,因为它是第一个分支的默认名称,但这并不能解决问题,因为此时将创建的其他分支具有未知名称。

在这种情况下,您可以使用任何配置条目来设置分支的默认推送目标吗?

【问题讨论】:

    标签: git set push config default


    【解决方案1】:

    您可以像这样将push.default 设置为当前:

    git config push.default current
    

    在man git-config中有描述:

    push.default
      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:
      (...)
    
      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.
    

    要检查它在实践中是如何工作的,首先添加一个远程存储库 创建新的本地存储库后将推送到,它甚至可以驻留在 相同的文件系统:

    $ git remote add origin <REMOTE_REPO_ADDRESS>
    

    然后尝试git push - 首先使用master 分支,然后使用dev:

    $ touch a
    $ git add a
    $ git commit -minitial
    [master (root-commit) c89b8e4] initial
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 a
    $ git push
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 206 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To /tmp/git-init-remote-
     * [new branch]      master -> master
    $ git checkout -b dev
    Switched to a new branch 'dev'
    $ touch b
    $ git add b
    $ git commit -mdev
    $ git push
    Total 0 (delta 0), reused 0 (delta 0)
    To /tmp/git-init-remote-
     * [new branch]      dev -> dev
    

    【讨论】:

    • 效果很好。注意:您需要将远程名称指定为origin。如果名称与origin 不同,则它将起作用,但仅适用于主分支。推送后创建的所有其他人都会收到错误fatal: No configured push destination.
    猜你喜欢
    • 2014-05-22
    • 2011-04-05
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    相关资源
    最近更新 更多