我不是一个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 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拉是否自动引用任何分支。
如果你运行命令,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没有配置,接下来的步骤已经在其他答案中解释过了。
在[分支"master"]下,尝试将以下内容添加到repo的Git配置文件(.git/config):
[branch "master"]
remote = origin
merge = refs/heads/master
这告诉Git两件事:
当您在主分支上时,默认的远程是origin。
在没有指定remote和branch的情况下,在主分支上使用git pull时,使用默认的remote (origin)并合并来自远程主分支的更改。
不过,我不确定为什么这个设置会从您的配置中删除。你可能也必须遵循其他人发布的建议,但这可能有用(或至少有帮助)。
如果你不想手动编辑配置文件,你可以使用命令行工具:
$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master