在 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

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

Merge local master branch with remote master branch in Git

gitgit-merge

提问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/mastercontains all of the commits that the local mastercontains, simply do a git pull:

如果remote/master包含本地包含的所有提交master,只需执行以下操作git pull

git checkout master
git pull remote master

You can check if the local masterhas commits that remote/masterdoesn'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 masterbut not in remote/master. If you don't see any output, that means remote/masterhas everything that the local masterhas.

这将显示包含在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 mastercontains commits that remote/masterdoesn'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 mergeor rebasethe local masterwith remote/master:

如果您想保留它们,您可以mergerebase局部masterremote/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 masterto 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 章。