gpt4 book ai didi

git - Git 推送的默认远程

转载 作者:行者123 更新时间:2023-12-05 01:46:09 24 4
gpt4 key购买 nike

Git-config你可以看到:

branch.name.remote

When on branch <name>, it tells git fetch and git push which remote to fetch from/push to. The remote to push to may be overridden with remote.pushDefault (for all branches). The remote to push to, for the current branch, may be further overridden by branch.<name>.pushRemote. If no remote is configured, or if you are not on any branch, it defaults to origin for fetching and remote.pushDefault for pushing.

现在我有一个克隆 存储库和一个名为test 的分支,并且是checkouted。在这里您可以看到克隆存储库的 config 文件的内容:

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = ...
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

如你所见,branch.test.pushRemote, remote.pushDefaultbranch.test.remote 没有在那里设置。所以我希望当我这样做的时候

$ git push

我会得到

fatal: No configured push destination.

但是我明白了

Everything up-to-date

似乎 Git 使用 origin 而不是未配置的 remote.pushDefault。但是为什么当文档说

it defaults to remote.pushDefault for pushing.

编辑:

Git-push你可以看到:

When the command line does not specify where to push with the <repository> argument, branch.*.remote configuration for the current branch is consulted to determine where to push. If the configuration is missing, it defaults to origin.

它认为使用originremote.pushDefault之间存在冲突。

最佳答案

Git 的推送代码(实际上也包括 pull 代码)(在我看来)过于复杂,因为它试图保留大量历史行为,其中一些是糟糕的想法。

我们先来看the REMOTES section of the git-push documentation :

The name of one of the following can be used instead of a URL as repository argument:

  • a remote in the Git configuration file: $GIT_DIR/config,

  • a file in the $GIT_DIR/remotes directory, or

  • a file in the $GIT_DIR/branches directory.

All of these also allow you to omit the refspec from the command line because they each contain a refspec which git will use by default.

(远程的名称——再一次,这只是我的意见——其中唯一真正应该保留至今的名称。我认为指定的 URL 方法可能应该移动到管道命令,并且 git push可以恢复为一个简单的脚本,虽然 Git-on-Windows 的人,唉,由于 Windows 的性能问题,试图从脚本中转换。)

幸运的是,您使用的是 named-remote 方法,所以我们可以忽略其中的大部分内容。具体来说,refspec 是可选的,因为您在 $GIT_DIR/config 中使用 Remote .

接下来,我们应该清楚地区分语法(名词和动词以及您在命令行中输入的内容)和语义(行为)。一旦我们扔掉了额外的“远程”变种,我们就剩下两个任务:

  1. 选择一个 Remote 。
  2. 选择引用规范。

您使用的语法git push ,即没有指定 repository 参数且未指定 refspec 。因此对于第 1 步,选择远程,Git 使用您在编辑中引用的段落:找到当前分支——在本例中,test –然后查找branch.test.remote .这没有配置(“缺失”),所以 Git 回退到使用 origin .

(引用的文档段落中有一个错误,因为它没有提到 branch.<em>branch</em>.pushRemote ,在这种情况下应该是 branch.test.pushRemote 。正确的顺序是(1)寻找特定于分支的 pushRemote;(2)查找分支特定的远程;(3) 如果 Git 版本为 1.8.3 或更高版本,则查找 remote.pushDefault;(4) 尝试使用 origin 这个词。此文档错误仍在 git 2.8.1 中。)

origin 是一个有效的远程名称,第 1 步成功,我们继续第 2 步,选择一个 refspec。这部分没有你疑惑,只是为了完整性:

When the command line does not specify what to push with refspec ... arguments or --all, --mirror, --tags options, the command finds the default refspec by consulting remote.*.push configuration, and if it is not found, honors push.default configuration to decide what to push (See git-config(1) for the meaning of push.default).

所以在这种情况下,Git 会查找 remote.origin.push并且,由于未设置,push.default .如果push.default未设置,有一个“默认默认值”,在 2.0 之前的 Git 中是 matching现在是(Git 2.0 或更新版本)simple .这个默认 push.default意味着总是一个refspec。

默认远程名称 origin意味着通常有一个 Remote 。仅当没有 [remote "origin"] 时才会失败您的 $GIT_DIR/config 部分.

关于git - Git 推送的默认远程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37146768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com