2

I start to work on some changes on my master branch and realize that this is not a quick fix and I will need more time to do my fix. So I save my work in a different branch as described here with:

git checkout -b newClientID

I didn't commit my change there, since I wanted to make more relevant progress. Then I switch to my master and reset it

git checkout master
git checkout -- .

When I switch back to my branch newClientID I realize that my uncommitted changes have been lost. Any chance to recover them?

magdmartin
  • 1,712
  • 3
  • 20
  • 43
  • In the future, if you want to save changes without committing, you'll probably want to consider using `git stash` – acjay Aug 26 '13 at 20:17

2 Answers2

3

If you don't commit and reset the working tree, it's lost forever.

Next time, commit often, maybe more than necessary and eventually squash commits with interactive rebasing before pushing to remote.

By commiting, e.g. with git commit -m wip, I find it less error prone than git stash because it's all too easy to git stash pop in the wrong branch.

Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
  • I guess I should have used git stash before changing branch. http://git-scm.com/book/en/Git-Tools-Stashing – magdmartin Aug 26 '13 at 20:17
  • Well I prefer `git commit -m wip` to `git stash` because it's too easy to `git stash pop` to the wrong branch – Gregory Pakosz Aug 26 '13 at 20:17
  • sorry... happened to me once, was on a mac, Time Machine backup saved my ass – Gregory Pakosz Aug 26 '13 at 20:19
  • I faced that problem either. Lucky, I'm using Java and IntellijIDEA, so I had an ability to get the changes from my compiled classes of target repository. Never the less you can try to get your changes from git local history. – barbariania Dec 23 '16 at 02:05
  • In this scenario, if i didn't use reset/restore, would I be able to retrieve my changes? – rupinderjeet Dec 24 '19 at 09:30
0

The same happened to me. this is a silly answer but it actually worked. I went to my files with the lost changes, ctrl+z and all the changes appeared.

YTG
  • 956
  • 2
  • 6
  • 19