Recover lost changes due to incorrect git usage
I am facing a situation where I lost set of changes I made to a few files while working in my branch in git. Unfortunately I realized that I have lost my set of changes a few hours after doing a series of git operations which I suspect would have caused loss of data. Here is the steps which I suspect would have caused data loss.
I am working on branch : feature/logging
In this branch I modify files:
- log.py
- tests/test_log.py
These files are available on other branches too. I have neither staged nor committed these files.
3.I now try to pull the changes that开发者_JAVA百科 are done in the remote repository by running:
%git pull origin
This results in a merge and there is a merge conflict in a few files (not necessarily the ones I am modifying) and merge is unsuccessful. I decide that I don't want to resolve the merge conflict now and abort the merge by running :
%git reset --hard HEAD
I think this step caused the content of files 'log.py' and 'tests/test_log.py' to be pulled from the previous commit and overwrite my uncommitted and unstaged changes.
Now my questions are :
- Am I correct in my assumption that the cause of data loss is the reset done to abort the merge ?
- If the reset was the cause of data loss is there a way to to get back my uncommitted/unresolved changes ?
Yes, git reset --hard
will indiscriminately destroy all local changes and restore to the last commit. But this should be the commit you were at before you did git pull
(provided you didn't have any unstaged changes at this point); so things might not be too bad.
git pull can be a little hairy. There's a good article on why you should consider doing git fetch
and git merge
as separate operations instead.
If anyone made the same blunder like me git reset --hard
before adding unstaged files then there are still chances to get those changes back. Although these files are not available in repo anymore but some of the new IDEs maintain their own history. Like in my case I was able to retrieve my unstaged changes from Android Studio's local history feature which is located under VCS. Directions to Revert Changes
精彩评论