[英]Merge local master branch with remote master branch in Git
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? 如何将主服务器的本地副本与远程主服务器的最新更改合并?
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
. 这将显示包含在
master
但不包含在remote/master
所有提交。 If you don't see any output, that means remote/master
has everything that the local master
has. 如果您没有看到任何输出,则表示
remote/master
具有本地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. 如果本地
master
包含remote/master
服务器不包含的提交,则必须弄清楚如何处理该提交。 Do you want to keep them and merge them with remote/master
, or do you simply want to throw them away? 你想保留它们并将它们与
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
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
You can read more about all of these commands from the Git documentation . 您可以从Git文档中阅读有关所有这些命令的更多信息。 I also highly recommend the excellent free online Pro Git book , especially chapters 1-3 and 6-6.5.
我也强烈推荐优秀的免费在线Pro Git书 ,尤其是第1-3章和第6-6.5章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.