git pull master 的问题与 origin master 不同步

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18907689/
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-19 09:05:00  来源:igfitidea点击:

Issue with git pull master is out of sync with origin master

gitgit-branchgit-pullgit-fetch

提问by user1769790

These are the sequence of steps I have performed:

这些是我执行的步骤序列:

  1. committed my changes in branch to local master (commit id dc9afg2k)
  2. git fetch origin master && git merge origin master
  3. git checkout master
  4. git pull(this pulled all recent changes)
  5. git fetch origin master && git merge origin master
  6. git reset --hard origin/master
  7. git checkout branch
  8. git blog
  9. git reset --hard dc9afg2k(commit successful)
  10. git checkout master
  11. git log(this was gone back to 2 days ago).
  12. git pull(masteris not updating with current origin/master).
  1. 将我在分支中的更改提交给本地主节点(commit id dc9afg2k
  2. git fetch origin master && git merge origin master
  3. git checkout master
  4. git pull(这拉动了所有最近的变化)
  5. git fetch origin master && git merge origin master
  6. git reset --hard origin/master
  7. git checkout branch
  8. git blog
  9. git reset --hard dc9afg2k(提交成功)
  10. git checkout master
  11. git log(这又回到了 2 天前)。
  12. git pullmaster不是用当前更新origin/master)。

回答by VonC

An out of sync mastercan happen when the remote repo has received a forced push(git push --force) which rewrite the history.

master当远程存储库收到重写历史记录的强制推送( git push --force)时,可能会发生不同步。

If you have done commits of your own on master:

如果您已经在 master 上完成了自己的提交:

  • make a branch (to remember the current master state)
    git branch old_master

  • make sure you don't have any private file you need to save.

  • follow this guide

  • 创建一个分支(记住当前主状态)
    git branch old_master

  • 确保您没有任何需要保存的私人文件。

  • 遵循本指南

That would be:

那将是:

git fetch origin
git reset --hard origin/master
git clean -f -d

(you can preview the last cleaning steap with a '-n' option: git clean -n -f -d)

(你可以用一个“预览上次清洗STEAP -n”选项:git clean -n -f -d



Note that git fetch origin master && git merge origin mastercould be a git pull origin master: the interest of keeping the two steps separated is to look at the difference between masterand origin/masterbefore the merge.
If you don't make that diff, then a git pullis simpler.

请注意,这git fetch origin master && git merge origin master可能是git pull origin master: 将两个步骤分开的目的是查看合并之前master和之间的差异origin/master
如果你不做那个差异,那么 agit pull更简单。