简体   繁体   English

如何将远程master合并到本地分支

[英]How to merge remote master to local branch

I have a local branch of a project ("configUpdate") that I've forked from somebody else's project and I've done a load of changes on it and would like to merge the changes they've made in to my local branch.我有一个项目的本地分支(“configUpdate”),我从其他人的项目分叉出来,我已经对其进行了大量更改,并希望将他们所做的更改合并到我的本地分支。

I've tried我试过了

git pull --rebase origin configUpdate

but it hasn't grabbed the latest changes - how can I merge the two?但它没有抓住最新的变化 - 我怎样才能合并两者? (also for bonus points what did I do with the pull --rebase command?) (还有奖励积分我用pull --rebase命令做了什么?)

From your feature branch (eg configUpdate ) run:从您的功能分支(例如configUpdate )运行:

git fetch
git rebase origin/master

Or the shorter form:或者更短的形式:

git pull --rebase

Why this works:为什么这样做:

  • git merge branchname takes new commits from the branch branchname , and adds them to the current branch. git merge branchname需要从分支新提交branchname ,并将它们添加到当前分支。 If necessary, it automatically adds a "Merge" commit on top.如有必要,它会自动在顶部添加“合并”提交。

  • git rebase branchname takes new commits from the branch branchname , and inserts them "under" your changes. git rebase branchname需要从分支新提交branchname更改“下”,将它们插入。 More precisely, it modifies the history of the current branch such that it is based on the tip of branchname , with any changes you made on top of that.更准确地说,它会修改当前分支的历史记录,使其基于branchname的提示,以及您在此基础上branchname任何更改。

  • git pull is basically the same as git fetch; git merge origin/master git pullgit fetch; git merge origin/master基本相同git fetch; git merge origin/master git fetch; git merge origin/master . git fetch; git merge origin/master

  • git pull --rebase is basically the same as git fetch; git rebase origin/master git pull --rebasegit fetch; git rebase origin/master基本相同git fetch; git rebase origin/master git fetch; git rebase origin/master . git fetch; git rebase origin/master

So why would you want to use git pull --rebase rather than git pull ?那么为什么要使用git pull --rebase而不是git pull呢? Here's a simple example:这是一个简单的例子:

  • You start working on a new feature.您开始研究新功能。

  • By the time you're ready to push your changes, several commits have been pushed by other developers.当您准备好推送更改时,其他开发人员已经推送了几个提交。

  • If you git pull (which uses merge), your changes will be buried by the new commits, in addition to an automatically-created merge commit.如果您使用git pull (使用合并),除了自动创建的合并提交之外,您的更改将被新提交掩埋。

  • If you git pull --rebase instead, git will fast forward your master to upstream's, then apply your changes on top.如果您改为git pull --rebase ,git 会将您的 master 快进到上游,然后将您的更改应用到顶部。

I found out it was:我发现它是:

$ git fetch upstream
$ git merge upstream/master

Switch to your local branch切换到本地分支

> git checkout configUpdate > git checkout 配置更新

Merge remote master to your branch将远程 master 合并到您的分支

> git rebase master configUpdate > git rebase 主配置更新

In case you have any conflicts, correct them and for each conflicted file do the command如果您有任何冲突,请更正它们并对每个冲突的文件执行命令

> git add [path_to_file/conflicted_file] (eg git add app/assets/javascripts/test.js) > git add [path_to_file/conflicted_file] (例如 git add app/assets/javascripts/test.js)

Continue rebase继续变基

> git rebase --continue > git rebase --continue

git rebase didn't seem to work for me. git rebase 似乎对我不起作用。 After git rebase, when I try to push changes to my local branch, I kept getting an error ("hint: Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes (eg 'git pull ...') before pushing again.") even after git pull.在 git rebase 之后,当我尝试将更改推送到我的本地分支时,我不断收到错误消息(“提示:更新被拒绝,因为您当前分支的提示落后于其远程对应分支。集成远程更改(例如 'git pull 。 ..') 在再次推动之前。”) 即使在 git pull 之后。 What finally worked for me was git merge.最终对我有用的是 git merge。

git checkout <local_branch>
git merge <master> 

If you are a beginner like me, here is a good article on git merge vs git rebase.如果你和我一样是初学者,这里有一篇关于 git merge 与 git rebase 的好文章。 https://www.atlassian.com/git/tutorials/merging-vs-rebasing https://www.atlassian.com/git/tutorials/merging-vs-rebasing

This applies to developers using Visual Studio.这适用于使用 Visual Studio 的开发人员。

  1. Click Git menu > Manage Branches > remotes/origin单击 Git 菜单 > 管理分支 > remotes/origin
  2. Right-click master > Merge 'origin/master' into [local branch]右键单击 master > 将 'origin/master' 合并到 [local branch]

Note: master is called main in recent git repositories.注意:master 在最近的 git 存储库中被称为 main。

在此处输入图片说明

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

相关问题 如何检出远程分支,将其重新建立为本地主机,然后与本地主机合并? - How to checkout a remote branch, rebase it to local master and then merge with local master? Git:如何将本地分支合并到远程主分支 - Git: How can I merge local branch into remote master branch 将本地主分支合并到远程子分支 - Merge local master branch into remote sub branch 在Git中将本地主分支与远程主分支合并 - Merge local master branch with remote master branch in Git Eclipse Git(EGit)-在与本地分支进行错误合并和提交之后,如何将本地主服务器恢复为远程主服务器 - Eclipse Git (EGit) - How to revert local master back to remote master after a foul merge & commit with a local branch Git-如何将远程分支合并到远程主服务器 - Git - How to merge a remote branch into remote master 为什么合并后本地 master 分支比远程 master 领先一个? - Why is the local master branch one ahead of remote master after merge? 将本地分支合并到主站以外的远程分支? - Merge local branch into remote branch other than master? 合并在远程主分支上 - Merge on the remote master branch Git:如何将其他存储库的远程主分支合并到本地主分支而不污染它 - Git: how to merge other repository's remote master branch into local master without contaminating it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM