`git config` 和 `git push --set-upstream` 有什么区别

发布于 2024-12-09 04:36:01 字数 898 浏览 0 评论 0原文

问题

$ git remote add origin [email protected]:yourname/yourproject.git
$ git config remote.origin.push refs/heads/master:refs/heads/master
$ git push

和:

$ git remote add origin [email protected]:yourname/yourproject.git
$ git push origin master -u

第二个版本只是比第一个版本更新且短,还是有其他差异?

背景研究

从 Git 1.7.0 开始,您可以将 --set-upstream 选项与 git push 一起使用。根据 git push 手册:

-u, --set-upstream
    对于每个最新或成功推送的分支,添加上游
    (跟踪)参考,由无参数 git-pull(1) 和其他命令使用。为了
    更多信息,请参阅 git-config(1) 中的branch..merge。

Question

What's the difference between:

$ git remote add origin [email protected]:yourname/yourproject.git
$ git config remote.origin.push refs/heads/master:refs/heads/master
$ git push

and:

$ git remote add origin [email protected]:yourname/yourproject.git
$ git push origin master -u

Is the second version simply newer and shorter than the first version, or are there other differences?

Background Research

As of Git 1.7.0, you can use the --set-upstream option with git push. According to the git push manual:

-u, --set-upstream
    For every branch that is up to date or successfully pushed, add upstream
    (tracking) reference, used by argument-less git-pull(1) and other commands. For
    more information, see branch.<name>.merge in git-config(1).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹 2024-12-16 04:36:01

不,这些是非常不同的。第一个配置设置 remote..push 在未指定其他引用规范的情况下设置用于推送的默认引用规范。默认情况下,执行 git push origin 会将每个分支推送到具有匹配名称的分支,只要远程上已存在具有该名称的分支即可。完成后:

git config remote.origin.push refs/heads/master:refs/heads/master

...你会发现git push origin只会将master推送到master

如果推送成功,您引用的另一个命令 git push -u origin master 会设置两个不同的配置选项:

  • branch.master.remote 设置为 origin
  • branch.master.merge 设置为 refs/heads/master

这些本质上是说 masterorigin 应该被视为您的默认“上游”分支master 分支。它们最明显的效果是当您在 master 上时为 git pull 提供默认操作,但也可用于各种其他情况,例如提供有用的消息在 git status 的顶部,告诉您 masterorigin/master 的比较情况。但是,这些设置用于通知 git Pushgit Push origin 的默认操作,除非您还设置了配置选项< code>push.defaulttracking(或最近版本中的upstream)。

因此,作为一个非常近似的总结,设置 remote..push 会影响 git push 的默认操作,而 git push -u origin master< /code> 设置通常只影响 git pull 操作的配置选项。

No, these are very different. The first config setting, remote.<name>.push sets a default refspec for pushing if no other refspec is specified. By default, doing git push origin will push every branch to a branch with a matching name so long as a branch with that name already existed on the remote. After doing:

git config remote.origin.push refs/heads/master:refs/heads/master

... you will find that git push origin will just push master to master.

The other command you quote, git push -u origin master, sets two different config options if the push is successful:

  • branch.master.remote is set to origin
  • branch.master.merge is set to refs/heads/master

These essentially say that master in origin should be regarded as the default "upstream" branch of your master branch. Their most obvious effect is to provide a default action for git pull when you are on master, but are also used in a variety of other situations, such as providing the helpful message at the top of git status that tells you where master is compared to origin/master. These settings are not, however, used to inform the default action of git push and git push origin unless you have also set the config option push.default to tracking (or upstream in recent versions).

So, as a very approximate summary, setting remote.<name>.push affects the default action of git push, while git push -u origin master sets config options that usually just affect the action of git pull.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文