我不是一个git大师,但我已经用它工作了一段时间,有几个不同的项目。在每个项目中,我总是git克隆[存储库],从这一点上,我总是可以git拉,当然,只要我没有突出的变化。

最近,我不得不恢复到以前的分支,并且使用git签出4f82a29。当我再次准备拉的时候,我发现我必须把树枝放回主人的位置。现在,我不能使用一个直接的git拉,而是,必须指定git拉源主,这是讨厌的,并向我表明,我不完全理解正在发生什么。

有什么改变,不允许我做一个直接的git拉没有指定的起源主人,我如何改变它回来?

更新:

-bash-3.1$ cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[branch "master"]
[remote "origin"]
    url = git@github.com:user/project.git
    fetch = refs/heads/*:refs/remotes/origin/*

更新2:要清楚,我知道我原来的方法可能是不正确的,但我需要修复这个回购,这样我就可以简单地再次使用git拉。目前,git的pull结果是:

-bash-3.1$ git pull
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  ').
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 = 
    branch.master.merge = 
    remote..url = 
    remote..fetch = 

See git-config(1) for details.

我可以告诉git拉哪个分支合并,它工作正确,但git拉不工作,因为它原来在我的git签出之前。


当前回答

只是想添加一些信息,我们可以检查这个信息git拉是否自动引用任何分支。

如果你运行命令,git remote show origin,(假设origin是remote的缩写),git会显示这个信息,不管是否存在git pull的默认引用。

下面是一个示例输出。(摘自git文档)。

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/schacon/ticgit
  Push  URL: https://github.com/schacon/ticgit
  HEAD branch: master
  Remote branches:
    master                               tracked
    dev-branch                           tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

请注意它所显示的部分,本地分支配置为git拉。

在这种情况下,git pull将引用git pull origin master

最初,如果您已经使用git clone克隆了存储库,那么这些问题就会自动得到解决。但是如果你使用git remote add手动添加了一个远程,这些在git配置中是没有的。如果是这样的话,那么它显示“本地分支配置为'git pull':”的部分,将从git remote show origin的输出中消失。

如果git pull没有配置,接下来的步骤已经在其他答案中解释过了。

其他回答

不想编辑我的git配置文件,我遵循@mipadi的帖子中的信息,并使用:

$ git pull origin master

如果你愿意,你可以通过命令行设置这些选项(而不是编辑配置文件),如下所示:

  $ git config branch.master.remote origin
  $ git config branch.master.merge refs/heads/master

或者,如果你像我一样,并希望这是你所有项目的默认值,包括那些你可能在未来工作的项目,然后将其添加为全局配置设置:

  $ git config --global branch.master.remote origin
  $ git config --global branch.master.merge refs/heads/master
git branch --set-upstream master origin/master

这将添加以下信息到您的配置文件:

[branch "master"]
    remote = origin
    merge = refs/heads/master

如果你有分支。Autosetuprebase = always则它还将添加:

    rebase = true

还有一种配置Git的方法,它总是将等价的远程分支拉到当前签出到工作副本的分支。它被称为跟踪分支,gitready默认建议设置。

对于当前工作目录之上的下一个存储库:

git config branch.autosetupmerge true

对于所有未配置的Git存储库:

git config --global branch.autosetupmerge true

有点神奇,恕我直言,但在特定分支总是当前分支的情况下,这可能会有所帮助。

当你有分支的时候。autosetupmerge设置为true并且第一次签出一个分支,Git会告诉你如何跟踪相应的远程分支:

(master)$ git checkout gh-pages
Branch gh-pages set up to track remote branch gh-pages from origin.
Switched to a new branch 'gh-pages'

Git会自动推送到相应的分支:

(gh-pages)$ git push
Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 1003 bytes, done.
Total 6 (delta 2), reused 0 (delta 0)
To git@github.com:bigben87/webbit.git
   1bf578c..268fb60  gh-pages -> gh-pages

你眼前的问题是如何让它拉主人,你需要按它说的做。在分支配置中指定要提取的refspec。

[branch "master"]
    merge = refs/heads/master