你怎么得到git总是从特定的分支拉?

时间:2009-03-18 15:53:59

标签: git version-control

我不是一个git master,但是我已经和它一起工作了一段时间,有几个不同的项目。在每个项目中,我始终git clone [repository],从那时起,只要git pull,只要我没有突出的变化,当然。

最近,我不得不恢复到以前的分支,并使用git checkout 4f82a29。当我再次准备好拉,我发现我必须将我的分支设置回主人。现在,我无法使用直线git pull,而是必须指定git pull origin master,这很烦人,并向我表明我不完全了解发生了什么。

在没有指定原始主文件的情况下,哪些更改不允许我直接git pull,以及如何将其更改回来?

更新:

-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 pull。目前,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 pull要合并哪个分支,并且它可以正常工作,但git pull不能像我git checkout之前那样工作。

9 个答案:

答案 0 :(得分:726)

[branch "master"]下,尝试将以下内容添加到repo的Git配置文件(.git/config)中:

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

这告诉Git 2事情:

  1. 当你在主分支上时,默认遥控器就是原点。
  2. 在主分支上使用git pull时,如果未指定remote和branch,请使用默认远程(origin)并合并远程主分支中的更改。
  3. 我不确定为什么这个设置会从您的配置中删除。您可能必须遵循其他人发布的建议,但这可能有效(或至少有帮助)。

    如果您不想手动编辑配置文件,可以改用命令行工具:

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

答案 1 :(得分:139)

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

  $ 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

答案 2 :(得分:82)

git branch --set-upstream master origin/master

这会将以下信息添加到您的config文件中:

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

如果您有branch.autosetuprebase = always,那么它还会添加:

    rebase = true

答案 3 :(得分:50)

我发现在mipadi和Casey的答案中很难记住确切的git configgit branch参数,因此我使用这两个命令添加上游参考:

git pull origin master
git push -u origin master

这会将相同的信息添加到.git / config中,但我发现它更容易记住。

答案 4 :(得分:23)

Git pull结合了两个操作 - 从跟踪分支中的远程存储库中获取新提交,然后将它们合并到当前分支中。

当您签出特定提交时,您没有当前分支,只有HEAD指向您上次提交的提交。所以git pull没有指定所有参数。这就是为什么它不起作用。

根据您的更新信息,您要做的是恢复您的远程仓库。如果你知道引入bug的提交,最简单的处理方法是使用git revert来记录一个新的提交,它会撤消指定的bug提交:

$ git checkout master
$ git reflog            #to find the SHA1 of buggy commit, say  b12345
$ git revert b12345
$ git pull
$ git push

由于您想要更改服务器,我将假设您不需要重写历史记录来隐藏错误提交。

如果在合并提交中引入了错误,则此过程将不起作用。请参阅How-to-revert-a-faulty-merge

答案 5 :(得分:10)

我不想编辑我的git配置文件我按照@ mipadi的帖子中的信息使用了:

$ git pull origin master

答案 6 :(得分:9)

还有一种配置Git的方法,它总是拉动并将等效的远程分支推送到当前检出到工作副本的分支。它被称为跟踪分支git ready recommends setting by default

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

git config branch.autosetupmerge true

对于所有Git存储库,没有另外配置:

git config --global branch.autosetupmerge true

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

当您branch.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

答案 7 :(得分:4)

关于如何让它成为主人的直接问题,你需要做它所说的。在分支配置中指定要从中提取的refspec。

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

答案 8 :(得分:0)

只需添加一些信息,我们就可以检查此信息git pull是否自动引用任何分支。

如果运行命令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 pull配置的本地分支。

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

最初,如果您已使用git clone克隆了存储库,则将自动处理这些事情。但是,如果您使用git remote add手动添加了远程,则git config中会缺少这些。在这种情况下,git remote show origin的输出中将缺少显示“为'git pull':配置的本地分支”的部分。

如果git pull不存在任何配置,接下来的步骤已由其他答案解释。

相关问题