[英]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 pull
与git 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 --rebase
与git 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 的开发人员。
Note: master is called main in recent git repositories.注意:master 在最近的 git 存储库中被称为 main。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.