你怎么让git总是从一个特定的分支拉?

我不是一个混帐大师,但我已经有一段时间了,有几个不同的项目。 在每一个项目中,我总是把git clone [repository]从那个angular度,总是能够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 (eg '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之前。

[branch "master"] ,尝试添加以下到repo的Gitconfiguration文件( .git/config ):

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

这告诉Git 2的东西:

  1. 当你在主分支上时,默认的远程是原点。
  2. 在主分支上使用git pull时,如果没有指定远程和分支,则使用默认的远程(原始),并合并来自远程主分支的更改。

不过,我不确定为什么这个设置会从你的configuration中删除。 你也许必须遵循其他人发布的build议,但是这可能会起作用(或者至less有帮助)。

如果您不想手动编辑configuration文件,则可以使用命令行工具:

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

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

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

或者,如果您和我一样,并希望将其作为所有项目(包括将来可能使用的项目)的默认设置,请将其添加为全局configuration设置:

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

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

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

如果你有branch.autosetuprebase = always那么它也会添加:

  rebase = true 

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

 git pull origin master git push -u origin master 

这会将相同的信息添加到您的.git / config,但我觉得更容易记住。

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

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

根据您的更新信息,您要做的是恢复您的远程回购。 如果你知道引入bug的提交,最简单的方法是使用git revert ,它logging一个新的提交,它撤消了指定的错误提交:

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

由于您的服务器是您想要更改的,因此我会假设您不需要重写历史logging来隐藏错误提交。

如果错误是在合并提交中引入的,那么这个过程将不起作用。 请参阅如何恢复错误合并 。

不想编辑我的gitconfiguration文件我跟在@ mipadi的文章中的信息,并使用:

 $ git pull origin master 

还有一种configurationGit的方法,它总是将等价的远程分支拉到当前检出到工作副本的分支上。 它被称为跟踪分支, 默认情况下git readybuild议设置 。

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

 git config branch.autosetupmerge true 

对于所有未configuration的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 

你如何让它拉高手的直接问题,你需要做它说的。 指定从您的分支configuration拉的refspec。

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