0

I had a master branch with unstaged/uncommitted changes. These are the commands I ran.

git branch dev
git checkout master
git checkout .
git pull
git checkout dev
git fetch origin
git merge origin/master

All my changes have disappeared from the files in dev. I never got a conflict warning when merging with master. My commit history on both branches look exactly the same. I've tried git reflog to no avail.

hash6 HEAD@{0}: reset: moving to HEAD@{8}
hash1 HEAD@{1}: reset: moving to HEAD@{8}
hash2 HEAD@{2}: reset: moving to HEAD@{8}
hash1 HEAD@{3}: reset: moving to HEAD@{10}
hash3 HEAD@{4}: reset: moving to HEAD@{10}
hash5 HEAD@{5}: reset: moving to HEAD@{10}
hash4 HEAD@{6}: reset: moving to HEAD@{10}
hash2 HEAD@{7}: checkout: moving from master to dev
hash2 HEAD@{8}: checkout: moving from dev to master
hash2 HEAD@{9}: merge origin/master: Fast-forward
hash1 HEAD@{10}: checkout: moving from master to dev
hash2 HEAD@{11}: commit: commit-message
hash1 HEAD@{12}: checkout: moving from dev to master
hash1 HEAD@{13}: checkout: moving from master to dev
hash1 HEAD@{14}: pull: Fast-forward
hash3 HEAD@{15}: pull: Fast-forward
hash5 HEAD@{16}: reset: moving to origin/master
hash4 HEAD@{17}: clone: from git@github.com:my/my-repo

Is there any way to recover those uncommitted changes? I feel like they shouldn't have just disappeared without any warning, but I can't find them anywhere.

Brian
  • 53
  • 5

1 Answers1

1

When you run git checkout . your files are updated to match their index state. This means that local changes are lost. Unfortunately there's no way of getting them back (short from trying your luck with undelete tools).

git checkout . would not have been necessary in your case (merging). If you performed that operation because you needed a clean working directory, use git stash save + git stash pop the next time.


Note: unstaged changes are not committed and therefore they do not "belong" to any branch. That means that they can easily get lost and there is no easy way to get them back. Since there is no commit on any branch (or any ref), they will not show up in reflog either.

However, there might be a tiny chance that you can restore your files' content. Have you run git stash recently before throwing away your local changes? Or were you in the process of preparing a commit and have added changes in those files? If so, you can run git fsck, look for "dangling blobs", inspect them with git cat-file and save them to disk. git fsck --lost-found might also be helpful in your case, see How to recover files from missing tree and dangling blobs?, Recover dangling blobs in git, or Recover files after bad git reset --hard command

knittl
  • 246,190
  • 53
  • 318
  • 364
  • So even though I ran `git checkout .` in master, it still affects my branch because I merged? Unfortunately I never stashed or added. – Brian Mar 11 '20 at 12:40
  • No, it does not affect any branch. See the second last paragraph. As long as changes are not committed, tthy do not belong to any branch. Creating a new branch does *not* move any unstaged changes to this branch. They still only live in the working directory, not on any branch – knittl Mar 11 '20 at 12:47