如何将git拉入不属于当前的分支? [英] How to 'git pull' into a branch that is not the current one?

查看:107
本文介绍了如何将git拉入不属于当前的分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

master 分支上运行 git pull 时,它通常会从原点/主。我在一个名为 newbranch 的不同分支中,但是我需要运行一个执行 git pull origin / master 转换为 master 但我无法运行 git checkout 来更改选择的分支直到拉动完成。有没有办法做到这一点?

When you run git pull on the master branch, 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 pull from origin/master into master but I cannot run git checkout to change the selected branch until after the pull is complete. Is there a way to do this?

为了给出一些背景知识,存储库存储一个网站。我在 newbranch 中进行了一些更改,并通过将网站切换到 newbranch 来部署它们。现在,这些更改已合并到上游 master 分支中,我试图将网站切换回 master 分支以及。此时, newbranch origin / master 是相同的,但 master 滞后于 origin / master 并需要更新。问题是,如果我以传统的方式:

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

$ 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

我需要实现与上述相同的功能( git checkout master&& git pull ),但不要将工作目录更改为以前的修订版在这个过程中。

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.

推荐答案

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

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

这篇关于如何将git拉入不属于当前的分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆