git 为什么我需要一直做`--set-upstream`?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6089294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I need to do `--set-upstream` all the time?
提问by Ram Rachum
I create a new branch in Git:
我在 Git 中创建了一个新分支:
git branch my_branch
Push it:
推它:
git push origin my_branch
Now say someone made some changes on the server and I want to pull from origin/my_branch
. I do:
现在说有人在服务器上做了一些更改,我想从origin/my_branch
. 我愿意:
git pull
But I get:
但我得到:
You asked me to pull without telling me which branch you
want to merge with, and 'branch.my_branch.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 "my_branch"]
remote = <nickname>
merge = <remote-ref>
[remote "<nickname>"]
url = <url>
fetch = <refspec>
See git-config(1) for details.
I learned that I can make it work with:
我了解到我可以让它工作:
git branch --set-upstream my_branch origin/my_branch
But why do I need to do this for every branch I create? Isn't it obvious that if I push my_branch
into origin/my_branch
, then I would want to pull origin/my_branch
into my_branch
? How can I make this the default behavior?
但是为什么我需要为我创建的每个分支都这样做?如果我推my_branch
入origin/my_branch
,那么我想拉origin/my_branch
入,这不是很明显my_branch
吗?我怎样才能使它成为默认行为?
回答by Mark Longair
A shortcut, which doesn't depend on remembering the syntax for git branch --set-upstream
1is to do:
不依赖于记住git branch --set-upstream
1的语法的快捷方式是:
git push -u origin my_branch
... the first time that you push that branch. Or, to push to the current branch to a branch of the same name (handy for an alias):
...第一次推送那个分支。或者,将当前分支推送到同名分支(对于别名很方便):
git push -u origin HEAD
You only need to use -u
once, and that sets up the association between your branch and the one at origin
in the same way as git branch --set-upstream
does.
您只需要使用-u
一次,并origin
以与之前相同的方式在您的分支和分支之间建立关联git branch --set-upstream
。
Personally, I think it's a good thing to have to set up that association between your branch and one on the remote explicitly. It's just a shame that the rules are different for git push
and git pull
.
就个人而言,我认为必须明确地在您的分支和远程分支之间建立这种关联是一件好事。遗憾的是,git push
和git pull
的规则不同。
1It may sound silly, but I very frequently forget to specify the current branch, assuming that's the default - it's not, and the results are most confusing :)
1这可能听起来很傻,但我经常忘记指定当前分支,假设这是默认值 - 事实并非如此,结果最令人困惑:)
Update 2012-10-11: Apparently I'm not the only person who found it easy to get wrong! Thanks to VonCfor pointing out that git 1.8.0 introduces the more obvious git branch --set-upstream-to
, which can be used as follows, if you're on the branch my_branch
:
2012 年 10 月 11 日更新:显然我不是唯一一个发现容易出错的人!感谢VonC指出 git 1.8.0 引入了更明显的git branch --set-upstream-to
,如果您在分支上,可以按如下方式使用my_branch
:
git branch --set-upstream-to origin/my_branch
... or with the short option:
...或使用短选项:
git branch -u origin/my_branch
This change, and its reasoning, is described in the release notes for git 1.8.0, release candidate 1:
此更改及其原因在git 1.8.0 候选版本 1 的发行说明中进行了描述:
It was tempting to say
git branch --set-upstream origin/master
, but that tells Git to arrange the local branchorigin/master
to integrate with the currently checked out branch, which is highly unlikely what the user meant. The option is deprecated; use the new--set-upstream-to
(with a short-and-sweet-u
) option instead.
很想说
git branch --set-upstream origin/master
,但这告诉 Git 安排本地分支origin/master
与当前检出的分支集成,这极不可能是用户的意思。该选项已弃用;改用新的--set-upstream-to
(带有短而甜的-u
)选项。
回答by Zamith
You can make this happen with less typing. First, change the way your push works:
您可以通过减少输入来实现这一点。首先,更改推送的工作方式:
git config --global push.default current
This will infer the origin my_branch
part, thus you can do:
这将推断出该origin my_branch
部分,因此您可以执行以下操作:
git push -u
Which will both create the remote branch with the same name and track it.
这将创建具有相同名称的远程分支并跟踪它。
回答by cdunn2001
You can simply
你可以简单地
git checkout -b my-branch origin/whatever
in the first place. If you set branch.autosetupmerge
or branch.autosetuprebase
(my favorite) to always
(default is true
), my-branch
will automatically track origin/whatever
.
首先。如果您将branch.autosetupmerge
或branch.autosetuprebase
(我最喜欢的)设置为always
(默认为true
),my-branch
将自动跟踪origin/whatever
.
See git help config
.
见git help config
。
回答by Tzen
You can set upstream simpler in two ways. First when you create the branch:
您可以通过两种方式更简单地设置上游。首先,当您创建分支时:
git branch -u origin/my-branch
or after you have created a branch, you can use this command.
或者在你创建了一个分支之后,你可以使用这个命令。
git push -u origin my-branch
You can also branch, check out and set upstream in a single command:
您还可以在单个命令中分支、检出和设置上游:
git checkout -b my-branch -t origin/my-branch
My personally preference is to do this in a two-step command:
我个人的偏好是在两步命令中执行此操作:
git checkout -b my-branch
git push -u origin my-branch
回答by Tamlyn
This is my most common use for The fwor.
这是我对The fwor最常见的用法。
$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
$ fwor
git push --set-upstream origin master [enter/↑/↓/ctrl+c]
Counting objects: 9, done.
...
Also, it's fun to type swear words in your terminal.
此外,在终端中输入脏话也很有趣。
回答by Daniel
You can use:
您可以使用:
git config --global branch.autosetupmerge always
git config --global branch.autosetupmerge 总是
which will link the upstream branch each time you create or checkout a new branch.
每次创建或检出新分支时,它将链接上游分支。
See https://felipec.wordpress.com/2013/09/01/advanced-git-concepts-the-upstream-tracking-branch/
见https://felipec.wordpress.com/2013/09/01/advanced-git-concepts-the-upstream-tracking-branch/
This also works with branch.autosetuprebase
, if you follow a more rebase focused workflow, but don't use this unless you know what you're doing, as it will default your pull behavior to rebase, which can cause odd results.
这也适用于branch.autosetuprebase
,如果您遵循更侧重于变基的工作流程,但除非您知道自己在做什么,否则不要使用它,因为它会将您的拉取行为默认为变基,这可能会导致奇怪的结果。
回答by djanowski
By the way, the shortcut to pushing the current branch to a remote with the same name:
顺便说一下,将当前分支推送到同名远程的快捷方式:
$ git push -u origin HEAD
回答by Amrit Shrestha
I personally use these following alias in bash
我个人在 bash 中使用以下这些别名
in ~/.gitconfig file
在 ~/.gitconfig 文件中
[alias]
pushup = "!git push --set-upstream origin $(git symbolic-ref --short HEAD)"
and in ~/.bashrc or ~/.zshrc file
并在 ~/.bashrc 或 ~/.zshrc 文件中
alias gpo="git pushup"
alias gpof="gpo -f"
alias gf="git fetch"
alias gp="git pull"
回答by youngrrrr
If the below doesn't work:
如果以下不起作用:
git config --global push.default current
You should also update your project's local config, as its possible your project has local git configurations:
您还应该更新项目的本地配置,因为您的项目可能具有本地 git 配置:
git config --local push.default current
回答by youngrrrr
git branch --set-upstream-to=origin/master<branch_name>