git - "u"到底做了什么? "git push -u origin master"与 "git push origin master"

标签 git

尽管我尽了最大努力去理解它,但我显然不擅长使用 git。

来自 kernel.org对于 git push :

-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).

这是 branch.<name>.merge来自 git config :

branch.<name>.merge

Defines, together with branch.<name>.remote, the upstream branch for the given branch. It tells git fetch/git pull which branch to merge and can also affect git push (see push.default). When in branch <name>, it tells git fetch the default refspec to be marked for merging in FETCH_HEAD. The value is handled like the remote part of a refspec, and must match a ref which is fetched from the remote given by "branch.<name>.remote". The merge information is used by git pull (which at first calls git fetch) to lookup the default branch for merging. Without this option, git pull defaults to merge the first refspec fetched. Specify multiple values to get an octopus merge. If you wish to setup git pull so that it merges into <name> from another branch in the local repository, you can point branch.<name>.merge to the desired branch, and use the special setting . (a period) for branch.<name>.remote.

我成功地使用 github 设置了一个远程存储库,并成功地将我的第一个提交推送到它:

git push -u origin master

然后,我无意中成功地将我的第二次提交推送到我的远程存储库,使用:

git commit -m '[...]'

但是,错误地认为我必须再次推送到 origin来自 master ,我跑了:

# note: no -u
git push origin master

那是做什么的?好像一点作用都没有。我“撤消”了吗git push -u origin master

最佳答案

关键是“无参数 git-pull”。当你做 git pull从分支,不指定源远程或分支,git 查看 branch.<name>.merge设置以知道从哪里 pull 。 git push -u为您推送的分支设置此信息。

要查看差异,让我们使用一个新的空分支:

$ git checkout -b test

首先,我们在没有 -u 的情况下推送:

$ git push origin test
$ git pull
You asked me to pull without telling me which branch you
want to merge with, and 'branch.test.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:

    [branch "test"]
    remote = <nickname>
    merge = <remote-ref>

    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>

See git-config(1) for details.

现在如果我们添加 -u :

$ git push -u origin test
Branch test set up to track remote branch test from origin.
Everything up-to-date
$ git pull
Already up-to-date.

请注意,已设置跟踪信息,以便 git pull无需指定远程或分支即可按预期工作。

更新:奖励提示:

  • 正如马克在评论中提到的,除了 git pull此设置还会影响 git push 的默认行为.如果你养成了使用 -u 的习惯要捕获您打算跟踪的远程分支,我建议设置您的 push.default配置值为 upstream .
  • git push -u <remote> HEAD会将当前分支推送到 <remote> 上的同名分支(并设置跟踪,以便您可以在之后执行 git push)。

关于git - "u"到底做了什么? "git push -u origin master"与 "git push origin master",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23691963/

相关文章:

git-multimail 排除 git diff 和后续邮件

git-remote - 如何知道本地 repo 与远程 repo 不同,没有获取?

git 有一个损坏的丢失对象无法修复

git - 在 Git 中运行预提交 Hook 。有没有办法验证脚本是否正在运行?

git - 摸索 git 远程使用

android - 你如何更新到最新的分支而不必再次下载所有内容?

ruby-on-rails - 如何在该应用程序中显示 Rails 应用程序的 git 分支?

git - 创建一个 GitHub webhook,用于何时接受 pull 请求并将其 merge 到 master

git - 使用 GIT 获取文件的最后更改

git read-tree 删除了所有历史记录