21

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

我有以下情况:

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

我可以使用git pull从原点获取和合并,效果很好:

$ git pull
Already up-to-date.

我可以像这样从棍子上拉出来:

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

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

$ 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.

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

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.

4

1 回答 1

26

What does "your configuration file" mean here?

Your repo's configuration file, found at .git/config at 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.)

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

You can use the git config program 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.

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 pull from stick.

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

# 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 pull without any remote or refspec info, it'll fetch all the branches from stick, and merge in stick's master branch. Note that origin will not be the default remote anymore; to merge in origin's master branch, you'll have to use 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.

于 2010-02-25T16:02:15.703 回答