如何告诉 Git 总是拉主分支?

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

How to tell Git to always pull the master branch?

gitgit-branch

提问by Milan Babu?kov

I find git docs very cryptic regarding this issue. I want to do a simple thing, but it seems doing it is not simple at all.

我发现 git docs 关于这个问题非常神秘。想做一件简单的事,但似乎做起来一点都不简单。

I have the following situation:

我有以下情况:

$ git remote -v
origin  git://192.168.0.49/mnt/repos
stick   /mnt/titanium/podaci/repos

I can use git pullto fetch and merge from origin, and that works fine:

我可以使用git pull从原点获取和合并,并且工作正常:

$ git pull
Already up-to-date.

I can pull from sticklike this:

我可以像这样从棒中拉出:

$ git pull stick master
Already up-to-date.

However, when I pull from stickwithout the masterpart, I get this message:

但是,当我在没有部件的情况下从棒中拉出时,我收到以下消息:

$ git pull stick
From /mnt/titanium/podaci/repos
 * [new branch]      su2009  -> stick/su2009
You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me either.  Please
name which branch you want to merge on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details on the refspec.

If you often merge with the same branch, you may want to
configure the following variables in your configuration
file:

    branch.master.remote = <nickname>
    branch.master.merge = <remote-ref>
    remote.<nickname>.url = <url>
    remote.<nickname>.fetch = <refspec>

See git-config(1) for details.

Some things confuse me here. What does "your configuration file" mean here? Which file should I edit, and what exactly should I type in? What's nicknamein this case?

有些事情让我感到困惑。“你的配置文件”在这里是什么意思?我应该编辑哪个文件,我应该输入什么?在这种情况下,昵称是什么?

I would expect that what I'm trying to accomplish is very common, but I haven't been able to find a straight-to-the-point answer with an example.

我希望我想要完成的事情很常见,但是我无法通过示例找到直截了当的答案。

回答by mipadi

What does "your configuration file" mean here?

“您的配置文件”在这里是什么意思?

Your repo's configuration file, found at .git/configat the root of your repo. (There's also a per-user global config file at ~/.gitconfig, but you don't want to put repo-specific settings there.)

你的 repo 的配置文件,在.git/config你的 repo 的根目录中找到。(在 处还有一个每用户全局配置文件~/.gitconfig,但您不想将特定于存储库的设置放在那里。)

Which file should I edit, and what exactly should I type in?

我应该编辑哪个文件,我应该输入什么?

You can use the git configprogram to write configuration information, instead of entering it manually. However, if you want to do it manually, just open up .git/config-- the syntax is fairly straightforward.

您可以使用该git config程序写入配置信息,而无需手动输入。但是,如果您想手动完成,只需打开.git/config- 语法相当简单。

What's nickname in this case?

在这种情况下,昵称是什么?

Nickname, in this case, is the name of the remote -- so "stick". You don't have to worry about the remote.*options, as those have already been set up, but you do need to set the branch.*options. These options tell Git what to merge when doing a git pullfrom stick.

在这种情况下,昵称是遥控器的名称 - 所以“棒”。您不必担心remote.*选项,因为它们已经设置好了,但是您确实需要设置branch.*选项。这些选项告诉 Git 在执行git pullfrom stick时要合并的内容。

Say you want to merge in master from stick when doing a git pullfrom stick. You can do so like this:

假设您想在执行 from Stick 时从 Stick 合并 master git pull。你可以这样做:

# Sets stick as default remote for git pull.
# Note that origin will no longer be the default remote for git pull!
$ git config branch.master.remote stick

# Automatically merge in stick's master branch when doing a git pull
$ git config branch.master.merge refs/heads/master

So now, when you do a git pullwithout any remote or refspec info, it'll fetch all the branches from stick, and merge in stick's master branch. Note that origin will notbe the default remote anymore; to merge in origin's master branch, you'll have to use git pull origin master.

所以现在,当你在git pull没有任何远程或 refspec 信息的情况下执行 a 时,它会从棒中获取所有分支,并合并到棒的主分支中。请注意, origin 将不再是默认遥控器;要合并到 origin 的 master 分支,您必须使用git pull origin master.

If you don't want to change the default remote to stick, you'll have to continue using git pull stick master.

如果您不想将默认遥控器更改为坚持,则必须继续使用git pull stick master.