arcy
arcy

Reputation: 13133

Moved to different branch, lost changes - is there a way back?

I was working in the master branch and didn't realize it. I have hours of work that I did in that branch.

When I had finished what I wanted to commit, I went to GitHub Desktop v2.2.3 and noticed that I was in master. Before I had added them to staging, I listed the available branches and picked the one I wanted; it asked me what I wanted to do with the current changes, and I chose an option that said I wanted to "bring them with me".

Now I can't find my changes in either branch.

I went to the command line and entered "git reflog" and get the following:

e328567 (HEAD -> master, origin/master, origin/HEAD, origin/branchNumberOne, branchNumberOne) HEAD@{0}: checkout: moving from branchNumberOne to master
e328567 (HEAD -> master, origin/master, origin/HEAD, origin/branchNumberOne, branchNumberOne) HEAD@{1}: checkout: moving from master to branchNumberOne
e328567 (HEAD -> master, origin/master, origin/HEAD, origin/branchNumberOne, branchNumberOne) HEAD@{2}: reset: moving to HEAD
e328567 (HEAD -> master, origin/master, origin/HEAD, origin/branchNumberOne, branchNumberOne) HEAD@{3}: clone: from https://gitlab.myURL.com/aCompany/myProject.git

I'm not even sure which HEAD index to use to try to get my changes back; how do I figure that out? Or is there another way to recover the changes?

Upvotes: 8

Views: 16018

Answers (4)

Saurabh P Bhandari
Saurabh P Bhandari

Reputation: 6752

Quoting this from the GitHub Desktop help page,

Switching between branches

If you have uncommitted, saved changes, you'll need to decide what to do with your changes before you can switch branches. You can commit your changes on the current branch, stash your changes on the current branch, or bring the changes to your new branch.

Turns out that both options (highlighted above) internally perform stash operations for the respective branches.

The issue of GitHub Desktop discarding the commits during the switching operation has been previously reported here for v2.2.2.

If the uncommitted changes are not found using this, then you can switch to git CLI as suggested here and try listing the stashes ( make sure you are on the branch you want the uncommitted changes) by using the command git stash list. If the stash is available, then you could either pop or apply them depending on whether you want to remove the stash or preserve it for further use.

Note:

  • Logs under Help menu of GitHub Destkop App might provide more info on what happened behind the scenes.
  • Just in case, if the logs show that the stash operation failed (by discarding uncommitted changes), it might still be possible to recover the changes.

Upvotes: 8

arcy
arcy

Reputation: 13133

It turns out that Github Desktop stashes the changes if you change branches and don't select the option I was sure I had selected. So they were stashed, but I didn't realize it. I have the changes back now.

Thanks to all with helpful hints & comments. @saurabh P Bhandari, if you want to put your stash suggestion in an answer, I'll accept it and delete this one.

Upvotes: 1

CodeWizard
CodeWizard

Reputation: 142372

If you will see in your git reflog output *ALL the commits are pointing to the same SHA-1 which means that no changes were made on

This is due to the face that all your branches are pointing to the same SHA-1

Decide on which branch you want to be and check it out. As simple as that


Your problem is that you did not commit your changes. You will not be able to retrieve them unless you stashed them or if you don't have them somewhere like in your IDE history.

Upvotes: 0

Oskar
Oskar

Reputation: 96

Last time that happened to me IntelliJ saved my day, if you use any IDE you can undo changes on files you lost. Either by doing "undo" or using file history feature.

Upvotes: 6

Related Questions