我不是一个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的方法,它总是将等价的远程分支拉到当前签出到工作副本的分支。它被称为跟踪分支,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

其他回答

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

  $ 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配置或git分支参数,因为在mipadi和Casey的答案中,所以我使用这两个命令来添加上游引用:

git pull origin master
git push -u origin master

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

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

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

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。

还有一种配置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