您如何让 git 始终从特定分支中提取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/658885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:16:45  来源:igfitidea点击:

How do you get git to always pull from a specific branch?

gitversion-control

提问by David Smith

I'm not a git master, but I have been working with it for some time now, with several different projects. In each project, I always git clone [repository]and from that point, can always git pull, so long as I don't have outstanding changes, of course.

我不是 git 大师,但我已经使用它一段时间了,有几个不同的项目。在每个项目中,我总是git clone [repository]并且从那时起,总是可以git pull,只要我没有突出的变化,当然。

Recently, I had to revert to a previous branch, and did so with git checkout 4f82a29. When I was again ready to pull, I found that I had to set my branch back to master. Now, I can not pull using a straight git pullbut instead, have to specify git pull origin master, which is annoying, and indicates to me that I don't fully understand what is going on.

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

What has changed which does not allow me to do a straight git pullwithout specifying origin master, and how to I change it back?

发生了什么变化,不允许我在git pull不指定原点大师的情况下直接进行,以及如何将其改回来?

UPDATE:

更新:

-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/*

UPDATE 2: To be clear, I understand that my original method may have been incorrect, but I need to fix this repo so that I can simply use git pullagain. Currently, git pull results in:

更新 2:明确地说,我知道我原来的方法可能不正确,但我需要修复这个 repo,以便我可以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.

I can tell git pullwhich branch to merge, and it works correctly, but git pulldoes not work as it did originally before my git checkout.

我可以判断git pull要合并哪个分支,并且它可以正常工作,但git pull不能像在我的git checkout.

回答by mipadi

Under [branch "master"], try adding the following to the repo's Git config file (.git/config):

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

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

This tells Git 2 things:

这告诉 Git 2 件事:

  1. When you're on the master branch, the default remote is origin.
  2. When using git pullon the master branch, with no remote and branch specified, use the default remote (origin) and merge in the changes from the remote master branch.
  1. 当您在 master 分支上时,默认远程是 origin。
  2. git pullmaster 分支上使用时,没有指定 remote 和 branch,使用默认的 remote(origin)并合并来自远程 master 分支的更改。

I'm not sure why this setup would've been removed from your configuration, though. You may have to follow the suggestions that other people have posted, too, but this may work (or help at least).

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

If you don't want to edit the config file by hand, you can use the command-line tool instead:

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

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

回答by Head

If you prefer, you can set these options via the commmand line (instead of editing the config file) like so:

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

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

Or, if you're like me, and want this to be the default across all of your projects, including those you might work on in the future, then add it as a global config setting:

或者,如果你像我一样,希望它成为你所有项目的默认设置,包括你将来可能会使用的项目,然后将其添加为全局配置设置:

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

回答by cmcginty

git branch --set-upstream master origin/master

This will add the following info to your configfile:

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

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

If you have branch.autosetuprebase = alwaysthen it will also add:

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

    rebase = true

回答by Luke Sampson

I find it hard to remember the exact git configor git brancharguments as in mipadi's and Casey's answers, so I use these 2 commands to add the upstream reference:

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

git pull origin master
git push -u origin master

This will add the same info to your .git/config, but I find it easier to remember.

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

回答by Paul

Git pullcombines two actions -- fetching new commits from the remote repository in the tracked branches and then merging them into your current branch.

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

When you checked out a particular commit, you don't have a current branch, you only have HEAD pointing to the last commit you made. So git pulldoesn't have all its parameters specified. That's why it didn't work.

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

Based on your updated info, what you're trying to do is revert your remote repo. If you know the commit that introduced the bug, the easiest way to handle this is with git revertwhich records a new commit which undoes the specified buggy commit:

根据您更新的信息,您要做的是恢复远程存储库。如果您知道引入错误的提交,处理此问题的最简单方法是使用git revertwhich 记录一个新提交,该提交撤消指定的错误提交:

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

Since it's your server that you are wanting to change, I will assume that you don't need to rewrite history to hide the buggy commit.

由于您要更改的是您的服务器,因此我假设您不需要重写历史记录来隐藏有问题的提交。

If the bug was introduced in a merge commit, then this procedure will not work. See How-to-revert-a-faulty-merge.

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

回答by Bengt

There is also a way of configuring Git so, it always pulls and pushes the equivalent remote branch to the branch currently checked out to the working copy. It's called a tracking branch which git readyrecommends setting by default.

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

For the next repository above the present working directory:

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

git config branch.autosetupmerge true

For all Git repositories, that are not configured otherwise:

对于所有未以其他方式配置的 Git 存储库:

git config --global branch.autosetupmerge true

Kind of magic, IMHO but this might help in cases where the specific branchis always the current branch.

魔法种类,恕我直言,但在情况下,这可能有助于在特定分支始终当前分支

When you have branch.autosetupmergeset to trueand checkout a branch for the first time, Git will tell you about tracking the corresponding remote branch:

当你第一次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 will then push to that corresponding branch automatically:

然后 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

回答by Bengt

Not wanting to edit my git config file I followed the info in @mipadi's post and used:

不想编辑我的 git 配置文件,我遵循了@mipadi 帖子中的信息并使用了:

$ git pull origin master

回答by Ryan Graham

Your immediate question of how to make it pull master, you need to do what it says. Specify the refspec to pull from in your branch config.

你的直接问题是如何让它成为主,你需要按照它说的去做。在分支配置中指定要从中提取的 refspec。

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

回答by dr_dev

Just wanted to add some info that, we can check this info whether git pullautomatically refers to any branch or not.

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

If you run the command, git remote show origin, (assuming origin as the short name for remote), git shows this info, whether any default reference exists for git pullor not.

如果您运行命令 , git remote show origin(假设 origin 作为远程的短名称),git 会显示此信息,无论是否存在任何默认引用git pull

Below is a sample output.(taken from git documentation).

下面是一个示例输出。(取自 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)

Please note the part where it shows, Local branch configured for git pull.

请注意它显示的部分,为 git pull 配置的本地分支。

In this case, git pullwill refer to git pull origin master

在这种情况下,git pull将参考git pull origin master

Initially, if you have cloned the repository, using git clone, these things are automatically taken care of. But if you have added a remote manually using git remote add, these are missing from the git config. If that is the case, then the part where it shows "Local branch configured for 'git pull':", would be missing from the output of git remote show origin.

最初,如果您使用 git clone 克隆了存储库,这些事情会自动处理。但是,如果您使用 git remote add 手动添加了远程,则 git 配置中将缺少这些。如果是这种情况,那么git remote show origin.

The next steps to follow if no configuration exists for git pull, have already been explained by other answers.

如果不存在git pull, 的配置,接下来要执行的步骤已经在其他答案中进行了解释。