简体   繁体   English

如何将尚未合并到master中的远程分支拉到本地分支中?

[英]How to take a pull of a remote branch that has not been merged into master into a local branch?

There is a remote branch and I want to take a pull from that branch into one of my local branches. 有一个远程分支,我想从那个分支拉到我的一个本地分支。 How do I do that? 我怎么做?

What is wrong with this? 这有什么问题?

git checkout myLocalBranch
git pull remoteBranch myLocalBranch

git pull is used to update your current branch to the latest version on the remote. git pull用于将您当前的分支更新为远程服务器上的最新版本。 You can't just pull a branch into another one. 您不能只是将一个分支拉入另一个分支。 That's called merging! 这就是所谓的合并!

So you have to do this: 因此,您必须这样做:

git fetch origin
git merge origin/remoteBranch

and resolve the merge conflicts (if any). 并解决合并冲突(如果有)。 This will result in the state you asked for: your local branch will have the remote branch on top. 这将导致出现您要求的状态:您的本地分支将在顶部具有远程分支。

As @eckes pointed out in the comments (thanks!) you should first run git fetch origin in order to tell git about remoteBranch . 正如@eckes在评论中指出的(谢谢!),您应该首先运行git fetch origin ,以向git讲述remoteBranch

First lets see where your commands can be improved. 首先让我们看看您的命令可以改进的地方。

git checkout myLocalBranch

is used to switch to an existing branch named myLocalBranch. 用于切换到名为myLocalBranch的现有分支。 If myLocalBranch does not exist then it will result in an error. 如果myLocalBranch不存在,则将导致错误。

git pull remoteBranch myLocalBranch

Cannot be used to arbitrarily pull from any branch to any branch. 不能用于任意从任何分支拉到任何分支。 Syntax for pull command is the following where some-remote is generally 'origin' or any other configured remote repository. 以下是pull命令的语法,其中some-remote通常是“ origin”或任何其他已配置的远程存储库。

git pull <some-remote> myLocalBranch

will pull the branch on remote which is being tracked by myLocalBranch. 会将分支拉到myLocalBranch正在跟踪的远程站点上。


Coming to your problem which is to pull a specific branch from remote. 出现问题是要从远程拉特定分支。 So, you need to create a local branch, specify the remote repository and the remote branch which has to be tracked. 因此,您需要创建一个本地分支,指定远程存储库和必须跟踪的远程分支。

git checkout -b myLocalBranch origin/remoteBranch

Will create a new local branch with name myLocalBranch which will track remoteBranch of 'origin' repository. 将创建一个名为myLocalBranch的新本地分支,该分支将跟踪“原始”存储库的remoteBranch。 Now you have switched to myLocalBranch and the branch has been configured. 现在,您已切换到myLocalBranch,并且分支已配置。 Do a git pull which pull your work from remote. 进行git pull ,从远程拉动您的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pull而不是fetch - 意外地将远程master合并到本地分支 - pull instead of fetch - accidentally merged remote master into local branch 在远程合并了一个拉取请求后,将更改拉到本地分支时为什么会有冲突? - Why are there conflicts when pulling changes to a local branch after a pull request has been merged at the remote? 如何将远程主分支转换为本地开发分支? - How to take remote master branch changes into local develop branch? 如何将远程主服务器重置回尚未与git中的另一个分支合并的提交 - How to reset remote master back to a commit which has not been merged with another branch in git 如何检查master是否已合并到当前分支 - How do I check if master has been merged into current branch 已经与master合并的分支中的错误如何解决? - How to fix a bug in a branch after it has already been merged with master? 我如何知道一个分支是否已经合并到 master 中? - How can I know if a branch has been already merged into master? 合并后修复远程分支 - Fixing a remote branch after it has been merged 如果其他分支已合并到主分支,如何将我的分支合并到主分支 - How to merge my branch to master if some other branch has been merged into the master 如何将合并分支从主分支(Github)中取出? - How take a merged branch out of a master branch (Github)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM