简体   繁体   English

在Git中将本地主分支与远程主分支合并

[英]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? 如何将主服务器的本地副本与远程主服务器的最新更改合并?

Case 1: remote/master has everything that local master has 案例1: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具有的所有内容。

Case 2: local master has commits that remote/master doesn't have 案例2:本地主人提交了远程/主人没有的提交

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合并,或者你只是想扔掉它们?

Case 2a: merge/rebase local master commits into remote/master 情况2a:merge / rebase local master提交到remote / master

If you want to keep them, you can either merge or rebase the local master with 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 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 . 您可以从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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM