在 Git 中将本地 master 分支与远程 master 分支合并
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18116198/
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
Merge local master branch with remote master branch in Git
提问by Ricky
I'm in a strange situation: I have copied a directory (local repository) from a former developer on to my machine. There have been several commits to the remote master that are NOT in his local repository.
我处于一种奇怪的情况:我已将一个目录(本地存储库)从前开发人员复制到我的机器上。有几次提交给远程主控不在他的本地存储库中。
How do I merge the local copy of master with the most up-to-date changes of the remote master?
如何将 master 的本地副本与远程 master 的最新更改合并?
回答by
Case 1: remote/master has everything that local master has
案例一:remote/master 拥有本地 master 拥有的一切
If remote/master
contains all of the commits that the local master
contains, simply do a git pull
:
如果remote/master
包含本地包含的所有提交master
,只需执行以下操作git pull
:
git checkout master
git pull remote master
You can check if the local master
has commits that remote/master
doesn't by using the following:
您可以使用以下方法检查本地master
是否有未提交的提交remote/master
:
git fetch remote
git log --oneline --graph remote/master..master
That will show you all commits that are contained in master
but not in remote/master
. If you don't see any output, that means remote/master
has everything that the local master
has.
这将显示包含在master
但不包含在remote/master
. 如果您没有看到任何输出,则意味着remote/master
拥有本地master
拥有的一切。
Case 2: local master has commits that remote/master doesn't have
情况 2:本地 master 提交了 remote/master 没有的提交
If the local master
contains commits that remote/master
doesn't contain, you'll have to figure out how you want to handle that. Do you want to keep them and merge them with remote/master
, or do you simply want to throw them away?
如果本地master
包含remote/master
不包含的提交,您将必须弄清楚您想如何处理它。您想保留它们并将它们与 合并remote/master
,还是只是想将它们扔掉?
Case 2a: merge/rebase local master commits into remote/master
案例 2a:将本地 master 提交合并/rebase 到 remote/master
If you want to keep them, you can either merge
or rebase
the local master
with remote/master
:
如果您想保留它们,您可以merge
或rebase
局部master
有remote/master
:
git checkout master
git fetch <remote>
# Merge remote/master
git merge remote/master
# Or rebase local commits on top instead
git rebase remote/master
# Push the results
git push remote master
Case 2b: throw away local master commits
案例 2b:丢弃本地主提交
If you don't want to keep the local commits, then just do a hard reset of the local master
to the same point as the remote/master
:
如果您不想保留本地提交,则只需将本地硬重置master
到与 相同的点remote/master
:
git checkout master
git fetch remote
git reset --hard remote/master
Documentation
文档
You can read more about all of these commands from the Git documentation. I also highly recommend the excellent free online Pro Git book, especially chapters 1-3 and 6-6.5.
您可以从Git 文档中阅读有关所有这些命令的更多信息。我还强烈推荐优秀的免费在线 Pro Git 书籍,尤其是第 1-3 章和第 6-6.5 章。