如何'git pull'进入一个不是当前分支的分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18994609/
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 17:01:53  来源:igfitidea点击:

How to 'git pull' into a branch that is not the current one?

gitbranchgit-branch

提问by Malvineous

When you run git pullon the masterbranch, it typically pulls from origin/master. I am in a different branch called newbranch, but I need to run a command that does a git pullfrom origin/masterinto masterbut I cannot run git checkoutto change the selected branch until after the pull is complete. Is there a way to do this?

当您git pullmaster分支上运行时,它通常从origin/master. 我在另一个名为 的分支中newbranch,但我需要运行一个执行git pullfrom origin/masterinto的命令,mastergit checkout在 pull 完成之前我无法运行以更改所选分支。有没有办法做到这一点?

To give some background, the repository stores a website. I have made some changes in newbranchand deployed them by switching the website to newbranch. Now those changes have been merged upstream into the masterbranch, I am trying to switch the website back to the masterbranch as well. At this point, newbranchand origin/masterare identical, but masteris lagging behind origin/masterand needs to be updated. The problem is, if I do it the traditional way:

为了提供一些背景知识,存储库存储了一个网站。我newbranch通过将网站切换到newbranch. 现在这些更改已在上游合并到master分支中,我也在尝试将网站切换回master分支。在这一点上,newbranchorigin/master是相同的,但master有所滞后origin/master,需要更新。问题是,如果我以传统方式这样做:

$ git checkout master
   # Uh oh, production website has now reverted back to old version in master
$ git pull
   # Website is now up to date again

I need to achieve the same as above (git checkout master && git pull), but without changing the working directory to an earlier revision during the process.

我需要实现与上述 ( git checkout master && git pull)相同的效果,但在此过程中无需将工作目录更改为较早的版本。

采纳答案by jthill

You've got a worktree you don't want to touch, so use another one. Clone is cheap, it's built for this.

你有一个你不想碰的工作树,所以使用另一个。克隆很便宜,它就是为此而构建的。

git fetch origin master       # nice linear tree
git clone . ../wip -b master  # wip's `origin/master` is my `master`
cd ../wip                     # .
git pull origin origin/master # merge origin's origin/master
git push origin master        # job's done, turn it in.
cd ../main
rm -rf ../wip                 # wip was pushed here, wip's done

git checkout master           # payload

The problem with all the other answers here is, they don't actually do the pull. If you need the merge or rebase you've got pull set up for, you need another worktree and the above procedure. Otherwise just git fetch; git checkout -B master origin/masterwill do.

这里所有其他答案的问题是,它们实际上并没有拉动。如果您需要合并或变基,您已经设置了拉取设置,则需要另一个工作树和上述过程。否则就git fetch; git checkout -B master origin/master行了。

回答by Martin Peter

Straightforward: Updating from a remote branch into a currently not checked-out branch master:

直截了当:从远程分支更新到当前未签出的分支master

git fetch origin master:master

where originis your remote and you are currently checked out in some branch e.g. dev.

其中origin是您的遥控器,您目前正在某个分支中签出,例如dev

If you want to update your current branch in addition to the specified branch at one go:

如果您想一次性更新指定分支之外的当前分支:

git pull origin master:master

回答by Thomas

This is answered here: Merge, update, and pull Git branches without using checkouts

在这里回答: 合并、更新和拉取 Git 分支而不使用结帐

# Merge local branch foo into local branch master,
# without having to checkout master first.
# Here `.` means to use the local repository as the "remote":
git fetch . foo:master

# Merge remote branch origin/foo into local branch foo,
# without having to checkout foo first:
git fetch origin foo:foo

回答by Malvineous

As it turns out, the answer is deceptively simple:

事实证明,答案看似简单:

$ git fetch                           # Update without changing any files
$ git branch -d master                # Remove out-of-date 'master' branch
$ git checkout --track origin/master  # Create and check out up-to-date 'master' branch

This allows you to update the masterbranch without switching to it until afterit has been updated.

这使您可以更新master而无需切换到它,直到分支它已被更新。

回答by meagar

You're worried about something that cannot be fixed, as Git operations are not atomic. You will always have a hole where your working directory is half waybetween branches, even if you update master without first switching to it. This is why Git is not a deployment tool.

您担心无法修复的问题,因为 Git 操作不是原子操作。你将永远有一个洞,你的工作目录是中途分支之间,即使你不先切换到它更新的主人。这就是Git 不是部署工具的原因

Since you're not actually committing code in your production environment (I hope), you don't actually needto have a branch checked out. You can simply do a git fetchto update your remote refs, and then git checkout origin/masterto move the working directory directlyto the commit currently pointed to by origin/master. This will put you in a detached head state, but again, as you're not committing code, this doesn't matter.

由于您实际上并未在生产环境中提交代码(我希望如此),因此您实际上不需要签出分支。你可以简单地git fetch更新你的远程引用,然后git checkout origin/master将工作目录直接移动到当前指向的提交origin/master。这将使您处于分离状态,但同样,由于您没有提交代码,因此这无关紧要。

This is the smallest hole you're going to get, but as I said, a hole still exists; checkoutis not atomic.

这是你要得到的最小的洞,但正如我所说,一个洞仍然存在;checkout不是原子的。

回答by Philip Beber

You can use update-ref for this:

您可以为此使用 update-ref:

git fetch
git update-ref refs/heads/master origin/master
git checkout master

Note that this would throw away any local commits in the master branch. In your case there won't be any so this is okay. For other people trying to do this where there are local commits, I don't think it's possible, since merge can only be run on the current branch.

请注意,这会丢弃 master 分支中的任何本地提交。在你的情况下不会有任何所以这没关系。对于尝试在本地提交的情况下执行此操作的其他人,我认为这是不可能的,因为合并只能在当前分支上运行。

回答by Sebastian Oscar Lopez

Malvineous's solution work for me

Malvineous 的解决方案对我有用

$ git fetch                           # Update without changing any files
$ git branch -d master                # Remove out-of-date 'master' branch
$ git checkout --track origin/master  # Create and check out up-to-date 'master' branch
$ git fetch                           # Update without changing any files
$ git branch -d master                # Remove out-of-date 'master' branch
$ git checkout --track origin/master  # Create and check out up-to-date 'master' branch

Just in give the error

只是在给出错误


warning: not deleting branch 'master' that is not yet merged to
         'refs/remotes/origin/master', even though it is merged to HEAD.
error: The branch 'master' is not fully merged.
If you are sure you want to delete it, run 'git branch -D master'.

So i run with -D option

所以我用 -D 选项运行

thanks

谢谢

回答by Luzian

git fetch origin master:master

git fetch origin master:master

  • "Pulls" (actually fetches) master.
  • If you have changes on your masterthat aren't pushed yet, origin/masteris merged into your master.
  • If there are merge conflicts, you'll have to solve them first.
  • “拉”(实际上是取)master
  • 如果您有master尚未推送的更改,origin/master则会合并到您的母版中。
  • 如果存在合并冲突,则必须先解决它们。