我显然不擅长使用git,尽管我尽了最大努力去理解它。
来自kernel.org的git推送:
- u
——set-upstream
对于每个最新的或成功推送的分支,添加upstream(跟踪)引用,由无参数的git-pull(1)和其他命令使用。更多信息请参见branch.<name>。在git-config(1)中合并。
这是分支。<名称>。从git配置中合并:
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 '[...]'
然而,我错误地认为我必须再次从master推到原点,我运行:
# note: no -u
git push origin master
这有什么用?这似乎根本没有任何效果。我“撤销”git push -u origin master了吗?
关键是“无争议的拉”。当你从一个分支中进行git拉取时,没有指定源远程或分支,git会查看这个分支。合并设置,知道从哪里拉。Git push -u为你正在推送的分支设置这个信息。
为了看到区别,让我们使用一个新的空分支:
$ git checkout -b test
首先,我们不带-u进行push:
$ 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可以按预期工作,而无需指定远程或分支。
更新:奖励提示:
正如Mark在评论中提到的,除了git pull,这个设置也会影响git push的默认行为。如果您习惯于使用-u来捕获您打算跟踪的远程分支,我建议将您的push.default配置值设置为upstream。
git push -u <remote> HEAD将当前分支推到<remote>上同名的分支(同时设置跟踪,这样你就可以在那之后执行git push)。
关键是“无争议的拉”。当你从一个分支中进行git拉取时,没有指定源远程或分支,git会查看这个分支。合并设置,知道从哪里拉。Git push -u为你正在推送的分支设置这个信息。
为了看到区别,让我们使用一个新的空分支:
$ git checkout -b test
首先,我们不带-u进行push:
$ 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可以按预期工作,而无需指定远程或分支。
更新:奖励提示:
正如Mark在评论中提到的,除了git pull,这个设置也会影响git push的默认行为。如果您习惯于使用-u来捕获您打算跟踪的远程分支,我建议将您的push.default配置值设置为upstream。
git push -u <remote> HEAD将当前分支推到<remote>上同名的分支(同时设置跟踪,这样你就可以在那之后执行git push)。