尽管我尽了最大努力去理解它,但我显然不擅长使用 git。
从 kernel.org 到 git push
:
-u --set-upstream 对于每个最新或成功推送的分支,添加上游(跟踪)引用,由无参数的 git-pull(1) 和其他命令使用。有关更多信息,请参阅 git-config(1) 中的 branch.
以下是来自 git config
的 branch.<name>.merge
:
branch.
我成功地使用 github 建立了一个远程存储库,并且我成功地将我的第一个提交推送到了它:
git push -u origin master
然后,我无意中成功地将我的第二次提交推送到了我的远程存储库,使用:
git commit -m '[...]'
但是,错误地认为我必须从 master
再次推送到 origin
,我运行:
# note: no -u
git push origin master
那做了什么?它似乎根本没有任何效果。我“撤消”git push -u origin master
了吗?
I'm apparently terrible at using git, despite my best attempts to understand it.
- 我从来没有人把我塑造得这么好。
关键是“无参数的 git-pull”。当您从分支执行 git pull
时,没有指定源远程或分支,git 会查看 branch.<name>.merge
设置以了解从哪里拉取。 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
按预期工作而无需指定远程或分支。
更新:奖励提示:
正如 Mark 在评论中提到的,除了 git pull 这个设置还会影响 git push 的默认行为。如果您习惯使用 -u 来捕获您打算跟踪的远程分支,我建议您将 push.default 配置值设置为上游。
git push -u
git push -u origin master
… 是相同的:
git push origin master ; git branch --set-upstream master origin/master
如果您忘记了 -u
,请执行最后一条语句!
或者你可以强制它:
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
如果你让命令为你做这件事,它会选择你的错误,比如你输入了一个不存在的分支或者你没有git remote add
;虽然这可能是你想要的。 :)
master
只是一个例子:)
git branch master -u origin/master
details @ stackoverflow.com/a/2286030/790359
-u
选项,立即键入 git push -u
即可。
--setup-upstream
将被弃用:The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
git branch --set-upstream-to=origin/master master
更简单地说:
从技术上讲,-u
标志将跟踪引用添加到您要推送到的上游服务器。
这里重要的是,这使您可以在不提供任何参数的情况下执行 git pull
。例如,一旦您执行 git push -u origin master
,您可以稍后调用 git pull
,git 会知道您实际上是指 git pull origin master
。
否则,您必须输入整个命令。
-u
标志设置为 orgin master
,则每次下一次拉动都会引用它。如果我想更改 git pull
行为,我应该运行 git push -u origin some_other_branch
并且 git pull
现在将引用 some_other_branch
?谢谢!
git push -u origin master
将所有必要的 git bash 命令推入和拉入 Github:
git status
git pull
git add filefullpath
git commit -m "comments for checkin file"
git push origin branch/master
git remote -v
git log -2
如果要编辑文件,则:
edit filename.*
要查看所有分支及其提交:
git show-branch
不定期副业成功案例分享
git push <remote> <branch>
使事情明确。如果您离开远程或分支 git 将退回到分支配置设置,这些设置是使用git push -u
为您设置的。git push origin test
(没有-u
)之后 感到困惑。然后,您表明git push -u origin test
消除歧义。是不是有错别字,还是我只是再次变得密集?git push <remote> <branch>
是明确的时,我的意思是相对于依赖于分支配置的git push
。同样,git pull <remote> <branch>
是明确的,git pull
依赖于分支配置。使用-u
推送后,git push
和git pull
都将按预期工作。git push
的 common misconception - 除非您自己设置push.default
,否则git push
仅使用上游分支配置来决定推送到哪个远程,而不是要更新的远程分支。git push origin master
和另一端 IE 相同。git pull origin master
.. 因此,如果假设分支发生变化,那么您可以git push origin branch_name
与另一侧的 IE 相同。git pull origin branch_name