问题
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