Why 'git push'(without -f) overwrites the remote history?

半腔热情 提交于 2020-06-11 06:59:19

问题


I had one remote repository where I made some local commits(~6 commits) in let's say repo A and pushed those commits to GitHub and then I made some changes that I didn't commit or pushed.

Later I renamed that repo in GitHub and instead of update the references, I cloned to another directory, let's say local repo B, and I made more commits(~30 commits) and pushed them and I deleted the repo B.

Then I noticed that the repo A has unpushed changes, I didn't pull the new commits just committed the changes, and this is when the history diverged and pushed them with $ git push(without -f or --force) then got the following output:

...
remote: This repository moved. Please use the location:
remote: <new URL of the repo>
...
 + 3d93603...cec405b master -> master (forced update)

It replaced all the commits pushed from the local repo B

Later I managed to recover those commits with this using the GitHub's reflog.

My question is why this happened? I didn't know git can overwrite the remote history without using -f, also that means git pull could rewrite the history like git push did it?

I never touched my push.default config, my version is 2.17.1, this is the link to the GitHub API events of the repo and I haven't done any special changes to the .gitconfig if that helps.

Edit: I replicated the experiment and now it gives me errors like torek pointed out, at this point, I just think this was caused by a bug.

来源:https://stackoverflow.com/questions/61797976/why-git-pushwithout-f-overwrites-the-remote-history

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!