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 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 --hardwill indiscriminately destroy all local changes and restore to the last commit. But this should be the commit you were at before you didgit 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 fetchandgit mergeas separate operations instead.