git 如何将远程master合并到本地分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7200614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge remote master to local branch
提问by Martyn
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
命令做了什么?)
回答by Joey Adams
From your feature branch(e.g 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 branchbranchname
, and adds them to the current branch. If necessary, it automatically adds a "Merge" commit on top.git rebase branchname
takes new commits from the branchbranchname
, and inserts them "under" your changes. More precisely, it modifies the history of the current branch such that it is based on the tip ofbranchname
, with any changes you made on top of that.git pull
is basically the same asgit fetch; git merge origin/master
.git pull --rebase
is basically the same asgit fetch; git rebase origin/master
.
git merge branchname
从分支获取新提交branchname
,并将它们添加到当前分支。如有必要,它会自动在顶部添加“合并”提交。git rebase branchname
从 branch 获取新提交branchname
,并将它们插入“下”您的更改。更准确地说,它会修改当前分支的历史记录,使其基于 的提示branchname
,以及您在此基础上所做的任何更改。git pull
基本上与git fetch; git merge origin/master
.git pull --rebase
基本上与git fetch; git rebase origin/master
.
So why would you want to use git pull --rebase
rather than git pull
? Here's a simple example:
那么为什么要使用git pull --rebase
而不是git pull
?这是一个简单的例子:
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.If you
git pull --rebase
instead, git will fast forward your master to upstream's, thenapply your changes on top.
您开始研究新功能。
当您准备好推送更改时,其他开发人员已经推送了几个提交。
如果您
git pull
(使用合并),除了自动创建的合并提交之外,您的更改将被新提交掩埋。如果
git pull --rebase
相反,git 会将您的 master 快进到上游,然后将您的更改应用到顶部。
回答by Martyn
I found out it was:
我发现它是:
$ git fetch upstream
$ git merge upstream/master
回答by Serge Seletskyy
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] (e.g. 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
回答by AJC
git rebase didn't seem to work for me. 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 (e.g. 'git pull ...') before pushing again.") even after git pull. What finally worked for me was git merge.
git rebase 似乎对我不起作用。在 git rebase 之后,当我尝试将更改推送到我的本地分支时,我不断收到错误消息(“提示:更新被拒绝,因为您当前分支的提示落后于其远程对应分支。集成远程更改(例如 'git pull 。 ..') 在再次推动之前。”) 即使在 git pull 之后。最终对我有用的是 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. https://www.atlassian.com/git/tutorials/merging-vs-rebasing
如果你和我一样是初学者,这里有一篇关于 git merge 与 git rebase 的好文章。 https://www.atlassian.com/git/tutorials/merging-vs-rebasing