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

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

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

更改了哪些不允许我在没有指定origin master的情况下进行直接git pull ,以及如何将其更改回来?

更新:

-bash-3.1$ cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[branch "master"]
[remote "origin"]
    url = [email protected]: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之前那样工作。


#1楼

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

$ git pull origin master

#2楼

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

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

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 [email protected]:bigben87/webbit.git
   1bf578c..268fb60  gh-pages -> gh-pages

#3楼

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

git pull origin master
git push -u origin master

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


#4楼

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

  $ 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

#5楼

git branch --set-upstream master origin/master

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

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

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

    rebase = true

#6楼

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

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

#7楼

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

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

如果在合并提交中引入了错误,则此过程将不起作用。 请参阅如何还原故障合并


#8楼

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

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

这告诉Git 2的事情:

  1. 当您在主分支上时,默认远程是原点。
  2. 在主分支上使用git pull时,如果未指定remote和branch,请使用默认远程(origin)并合并远程主分支中的更改。

我不确定为什么这个设置会从你的配置中删除。 您可能必须遵循其他人发布的建议,但这可能有效(或至少有帮助)。

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

$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master
发布了0 篇原创文章 · 获赞 2 · 访问量 4982

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/104038479