Using Undo On Local Commits Can Lead to Data Loss
See original GitHub issueDescription
Data loss can happen if you use the ‘undo’ functionality on a reverted local commit. Noticed this when playing with #5873.
Version
- GitHub Desktop: 1.4.2
- Operating system: macOS 10.13.6
Steps to Reproduce
- Make a change to a file.
- Make a commit for this change, but do not push, so that it stays local.
- Go to History tab, right-click on the local commit, ‘Revert This Commit’. There are now 2 local commits that haven’t been pushed.
- Go to the Changes tab. Undo both commits.
- View file on disk, the change made in step 1 should be gone.
Expected Behavior
The change from step 1 should still be in the file. This is the behavior that happens if you simply skipped step 3 and used undo on the commit from step 2.
Actual Behavior
When you undo the revert commit, the Changes tab shows the change from step 1 for that file, as if it’s not committed by the commit from step 2, and the change is available to commit. I think this is the crux of the issue, as this is when things diverge between GitHub Desktop and the file on disk. If you only undo the revert commit, then view the file on disk, you’ll see that it’s back to the HEAD
version, without the change from the commit in step 2, but the change is visible under Changes. So the Changes tab at this point is not reflecting the file on disk. Undoing the original commit removes the changes from the Changes tab, so now it reflects what’s on disk, and now there’s data loss, as the change from step 1 is lost.
Additional Information
Logs
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:20 (5 by maintainers)
Top GitHub Comments
This issue is still going on! I just lost hours of work because of this, worse part is I didn’t even notice until I read this post. I just assumed GitHub Desktop was simply not seeing my changes, so I decided to google for it.
This is a very very bad bug. I don’t understand how can you have this software out there?
Apologies for not providing an update about this issue sooner. We’re exploring ways to make it easier to recover from these types of situations, but for the time being I’d like to provide some more context about what’s happening and how to recover from it.
Here’s the equivalent from the command line of what GitHub Desktop is doing when you follow the
revert
->undo
->undo
pattern:git revert <SHA_of_most_recent_commit>
git reset —mixed HEAD~1
git reset —mixed HEAD~1
The double-reset is what is causing the changes to be lost from your working directory. Thankfully Git makes it really hard to lose work that has been previously committed to your repository. Since this work was previously committed it can be recovered by running the
git reflog
command from the command line. After running that command you should see the specific SHA and the commit message of your commit that was lost during the undo process. The commit can then be recovered by creating a new branch off of the SHA found in the reflog. Here is the specific command you will need to run from the command line:git checkout -b recovery <SHA>
The
recovery
branch can then be merged back into your main branch in Github Desktop.If you have any questions about this process please let me know and I’d be happy to help further.