I am trying to reproduce a problem that resulted from my attempted answer to this question.
In short
A github user tried to do git pull --rebase
and that user's local files were deleted. I have tried to recreate this scenario on github but in my case nothing is deleted.
So how can I reproduce the scenario where git pull is destructive?
The details
Part 1:
- A user asked how to synchronize local changes to a newly-created remote repository.
git push
failed because the user did not have remote changes (as expected).- I suggested to use
git pull --rebase
, then resolve, thengit push
again. - But my suggestion resulted in the user's local files being deleted. (see original question)
- note that the files were tracked (committed locally), so the user was able to restore them.
Part 2:
- I created a repo called https: //github.com/user/myrepo, initialized with README and .gitignore
locally, I did:
initialized repo locally
> mkdir myrepo > cd myrepo > echo AAAAA > fileA.txt > echo BBBBB > fileB.txt > echo ignoreme > .gitignore > git init
committed the files locally
> git add . > git commit -m"initial commit"
tried to pull remote changes
> git remote add origin https://github.com/user/myrepo.git > git branch --set-upstream-to=origin/master master > git pull --rebase
the result was (as expected):
First, rewinding head to replay your work on top of it... Applying: initial commit Using index info to reconstruct a base tree... Falling back to patching base and 3-way merge... Auto-merging .gitignore CONFLICT (add/add): Merge conflict in .gitignore error: Failed to merge in the changes. Patch failed at 0001 initial commit The copy of the patch that failed is found in: .git/rebase-apply/patch When you have resolved this problem, run "git rebase --continue". If you prefer to skip this patch, run "git rebase --skip" instead. To check out the original branch and stop rebasing, run "git rebase --abort".
At this point, if I do
ls
, my local files are still on my filesystem.> ls -a . .. .git .gitignore README.md fileA.txt fileB.txt
Update:
Here is a comment from the original user who had the problem.
from user7342807:
... I installed wordpress this morning and did exactly this.
git init git add . git commit -m "initial commit"
at this point, I had realized I already created the github page for it, WITH a default readme in it.
So, not knowing this would be an issue, I tried to push as
git remote add origin http://github.com/user/repo.git git push -u origin master
This is when I received the message:
$ git push -u origin master To https://github.com/user/project.com ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/user/project.com' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
So, instead of force pushing and erasing that README file on github, I did
git pull --rebase
and git showed me this messageFirst, rewinding head to replay your work on top of it...
I waited about 5 minutes, before I
ctrl+C
out of the process, and realized there were about 8-9 files removed from the wordpress site.git status
also showed me the removed files.I was able to recover those files using
git reflog
which showed me my first commit on HEAD@1 and I recovered my files withgit reset --hard "HEAD@{5}"
What could have caused the files to be deleted from the other user's local filesystem?
Please note that there is a similar question that asks how to un-delete files when this happens. So this case occurs and people are losing their files.