git 配置新分支的默认推送目的地

git config default push destination for new branches

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

git init

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

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

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

在这种情况下,是否有任何配置条目可用于设置分支的默认推送目的地?

您可以像这样将 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