使用“git pull”时,如何将 Git 配置为自动从当前分支拉取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19279937/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:07:04  来源:igfitidea点击:

How do I configure Git to automatically pull from current branch when using "git pull"?

gitbranchconfig

提问by Brandon Durham

With our current setup you always have to enter the branch name (ie: git pull origin feature-branch" when doing a pull. I've already made the mistake of pulling from one branch into another, accidentally merging two branches with two very different releases. I'd like to avoid this by configuring Git so that simply typing git pullwill pull the current branch you're in.

使用我们当前的设置,您始终必须输入分支名称(即:git pull origin feature-branch“在进行拉取时。我已经犯了从一个分支拉入另一个分支的错误,不小心将两个分支与两个截然不同的版本合并。我想通过配置 Git 来避免这种情况,只需键入即可git pull拉出您所在的当前分支。

How do I do this?

我该怎么做呢?

采纳答案by Karthik Ramachandran

You can create a tracking branch. From the Git Book (http://git-scm.com/book/en/Git-Branching-Remote-Branches):

您可以创建一个跟踪分支。从 Git 书(http://git-scm.com/book/en/Git-Branching-Remote-Branches):

When you clone a repository, it generally automatically creates a masterbranch that tracks origin/master. That's why git pushand git pullwork out of the box with no other arguments. However, you can set up other tracking branches if you wish — ones that don't track branches on originand don't track the masterbranch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. If you have Git version 1.6.2 or later, you can also use the --trackshorthand:

$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "serverfix"

当您克隆存储库时,它通常会自动创建一个 master跟踪origin/master. 这就是为什么git push,并 git pull没有其他参数工作开箱。但是,如果您愿意,您可以设置其他跟踪分支——那些不跟踪分支origin和不跟踪master分支的分支。简单的例子就是你刚刚看到的例子,运行 git checkout -b [branch] [remotename]/[branch]. 如果你有 Git 1.6.2 或更高版本,你也可以使用--track简写:

$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "serverfix"

回答by batilc

I am, too, a fan of typing just git pulland getting all the magic.

我也喜欢打字git pull并获得所有魔法。

You have 2 options:

您有 2 个选择:

1)git config --global branch.autoSetupMerge always

1)git config --global branch.autoSetupMerge always

This will ensure that whether you checkout a remote branch, or create a new one; the tracking information will be handled automatically by git. Then you will be able to

这将确保您是否检出远程分支或创建一个新分支;跟踪信息将由 git 自动处理。然后你就可以

git clone <some_repo>
git checkout -b <new_branch>
git push
git pull

Note that in order to pushwithout more keywords, you need to set the push option as well. I have set it to matching, but everyone has their preference on that. (git config --global push.default matching)

请注意,为了push没有更多关键字,您还需要设置推送选项。我已将其设置为matching,但每个人对此都有自己的偏好。( git config --global push.default matching)

More info:autosetupmergedefaults to true. When set to true, this lets git to perform tracking when you checkout an already existing branch at the remote. For example, if you do git checkout <branch>, git will handle the tracking info so that you can do git pullwhile on that branch. However, it will not perform this on branches that you create with -boption. Setting autosetupmergeto alwaysensures that git handles tracking info all the time.

更多信息:autosetupmerge默认为true. 当设置为 true 时,这让 git 在您签出远程现有分支时执行跟踪。例如,如果您这样做git checkout <branch>,git 将处理跟踪信息,以便您可以git pull在该分支上进行。但是,它不会在您使用-b选项创建的分支上执行此操作。设置autosetupmergealways确保 git 始终处理跟踪信息。

2)When checking out a new branch, you need to specifically set the branch to pull from origin (aka tracking)

2)签出新分支时,需要专门设置分支从原点拉取(又名跟踪)

git checkout -b <branch> --track <remote>/<branch>

I find this less useful when the branches are transient. If you rarely create a new branch, you should go with this. However, if you are like me, where only the masterbranch is persistent and every feature has its own brand new branch, then I find option 1 more useful.

当分支是瞬态时,我发现这不太有用。如果您很少创建新分支,则应该使用它。但是,如果您像我一样,只有分支是持久的,并且每个功能都有自己的全新分支,那么我发现选项 1 更有用。

Note that, you do not need to make git configuration --global. You may simply write --localthere, and have that setting specific to that repository only.

请注意,您不需要进行 git configuration --global。您可以简单地--local在那里编写,并且仅针对该存储库进行该设置。

回答by Samuel

This worked for me:

这对我有用:

git branch --set-upstream-to=origin/branch_name branch_name

After doing this I can use the following syntax:

这样做后,我可以使用以下语法:

git checkout branch_name
git pull --rebase
git push

回答by ハセン

If you have git pushconfigured to automatically figure the remote branch name, then you can pass -uand it will also automatically set the tracking branch.

如果您git push配置了自动计算远程分支名称,那么您可以通过-u,它也会自动设置跟踪分支。

To setup git push to use a remote branch with the same name:

要设置 git push 以使用具有相同名称的远程分支:

git config --global push.default current`

Explanation for the -uoption from git help push:

-u选项说明git help 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).

Assuming your current branch is <branch_name>:

假设您当前的分支是<branch_name>

$ git push -u
Branch <branch_name> set up to track remote branch <branch_name> from origin.
Everything up-to-date

回答by ZzaichikK

Also, if you were to go into your .gitconfigfile and make one little change, you can set it to automatically assume you want to push/pull from the current branch in any project. So open .gitconfigin whatever editor you want. And find the [push] option, set it to default=simple, just like below.

此外,如果您要进入您的.gitconfig文件并进行一点更改,您可以将其设置为自动假定您要从任何项目的当前分支推/拉。所以.gitconfig在你想要的任何编辑器中打开。并找到 [push] 选项,将其设置为default=simple,如下所示。

[push]
        default = simple
[pull]
        default = simple

like that. Change pull to simple as well. Both are probably set to current right now. Essentially it's exactly the same option as I posted earlier:

像那样。将 pull 也改为 simple。两者现在可能都设置为当前。本质上,它与我之前发布的选项完全相同:

git config --global pull.default current

but I've found it to be a better option. So you could use this same line but change currentto simple.

但我发现它是一个更好的选择。因此,您可以使用同一行,但更改currentsimple.

回答by ZzaichikK

This command should configure git to pull to the current branch ..when run in terminal/iTerm.

当在终端/iTerm 中运行时,此命令应配置 git 以拉到当前分支。

git config --global pull.default current

Just change "pull" to "push" to get the same effect with "git push".

只需将“pull”更改为“push”即可获得与“git push”相同的效果。