如何将“Git”拉进不是当前分支的分支?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (14)

当你运行的时候git pullmaster分支,它通常从origin/master...。我在另一个分支叫做newbranch,但我需要运行一个命令来执行git pullorigin/mastermaster但我不能跑git checkout若要更改所选分支,直到完成拉出为止。有办法吗?

为了给出一些背景,存储库存储了一个网站。我在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

我需要达到和上面一样的目的(git checkout master && git pull),但是在过程中没有将工作目录更改为早期的修订。

提问于
用户回答回答于

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

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
用户回答回答于

# 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

扫码关注云+社区