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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:41:55  来源:igfitidea点击:

How to merge remote master to local branch

gitbranchpull

提问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 --rebasecommand?)

但它没有抓住最新的变化 - 我怎样才能合并两者?(还有奖励积分我用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 branchnametakes new commits from the branch branchname, and adds them to the current branch. If necessary, it automatically adds a "Merge" commit on top.

  • git rebase branchnametakes new commits from the branch branchname, and inserts them "under" your changes. 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.

  • git pullis basically the same as git fetch; git merge origin/master.

  • git pull --rebaseis basically the same as git 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 --rebaserather 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 --rebaseinstead, 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