Git 使用远程 Master 更新本地分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34656523/
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
Git Update Local Branch with remote Master
提问by sparkr
I see two possibilities of doing this:
我看到这样做的两种可能性:
Do a replace of the local branch with the changes from remote master
Follow the work flow that I get using Gitlab by creating a merge request and merge the changes from master branch into the branch that I wish to update to the latest from master
用远程主服务器的更改替换本地分支
按照我使用 Gitlab 获得的工作流程创建合并请求并将 master 分支中的更改合并到我希望从 master 更新到最新的分支中
What are the advantages and disadvantages of both these approaches? I'm leaning more towards the first approach. What do you guys say?
这两种方法的优缺点是什么?我更倾向于第一种方法。你们怎么说?
回答by DaveyDaveDave
The simple answer - there are plenty of more complicated ones - is to just do a merge, so:
简单的答案 - 有很多更复杂的 - 只是进行合并,因此:
git checkout master
git pull
git checkout <your-branch>
git merge master
(This is effectively the same as you describe in option 2)
(这实际上与您在选项 2 中描述的相同)
Depending on your settings, you might not need all of those steps (but doing them all won't hurt) - I'd recommend reading up on each of the commands to find the precise workflow that fits you best.
根据您的设置,您可能不需要所有这些步骤(但执行所有步骤不会有什么坏处) - 我建议您阅读每个命令以找到最适合您的精确工作流程。
This will merge the changes from master into your branch and probably create a new commit, with a comment making it clear that it's a merge.
这会将 master 的更改合并到您的分支中,并可能创建一个新的提交,并带有注释,明确表示这是一次合并。
The alternative, and slightly more advanced option would be to rebase
, instead of merge
, which will effectively rewind time to the point at which your branch diverged from master, then pull in the changes on master, bringing your branch in-line with master, but without your commits, and finally apply your commits at the end. The advantage of this is that it keeps the history more simple - you just get a straight line of changes, with the changes from your branch right at the end, rather than two separate branches that join at the point of the merge.
另一种稍微更高级的选项是rebase
, 而不是merge
,这将有效地将时间倒回到您的分支与 master 分歧的点,然后在 master 上拉入更改,使您的分支与 master 保持一致,但没有你的提交,最后应用你的提交。这样做的好处是它使历史记录更加简单——你只得到一条直线的变化,来自你的分支的变化就在最后,而不是在合并点连接的两个单独的分支。
To do that, you'd do:
要做到这一点,你会这样做:
git checkout <your-branch>
git rebase master
I'd recommend reading the docs on rebase, because there are lots of cases where it gets difficult, and if you're new to git, definitely go for merge, but come back to rebase when you're more confident - it's a very powerful feature, and more like what I think you're describing in your option 1.
我建议阅读有关 rebase 的文档,因为在很多情况下它会变得困难,如果您是 git 新手,一定要进行合并,但是当您更有信心时再回来 rebase - 这是一个非常强大的功能,更像是我认为您在选项 1 中所描述的。
回答by Pawe? Babicz
When you in your current branch git merge master
当您在当前分支时 git merge master
回答by Michael Chekin
If you remote
is set to the default origin
, (you can check it by using git remote -v
), you could just do:
如果您remote
设置为默认origin
, (您可以使用 来检查它git remote -v
),您可以这样做:
git merge origin master
回答by Renil Babu
You can rebase also:
你也可以变基:
git rebase origin/master