1

I checked out a previous commit. I made many changes for hours and committed them. Then I switched to a different branch. When I switched back to previous commit I was working on, I noticed all my changed are gone. I don't see them in the history either.

In other words, I did the following:

  1. git checkout bfea631 (a previous commit)
  2. Changed, added, and committed many files/changes.
  3. git checkout master
  4. Made a commit.
  5. git checkout bfea631 (a previous commit)
  6. Noticed my changes I made in step 2 are gone.

Any help would be appreciated as I spend multiple hours making changes in step 2 above.

AskYous
  • 4,332
  • 9
  • 46
  • 82
  • 1
    After your step 2. your new commit hash generated. check your git log you will find your commit – IMParasharG Mar 18 '19 at 22:04
  • Does this answer your question? [How can I recover a lost commit in Git?](https://stackoverflow.com/questions/10099258/how-can-i-recover-a-lost-commit-in-git) – Joe Sep 03 '21 at 04:07

3 Answers3

3

You can try to find your commit using git reflog. It shows where your HEAD has been lately.

WofWca
  • 571
  • 3
  • 11
0

Why did it happen?

After checking a commit directly like you did, your HEAD isn't attached to a branch any more. (known as a "detached HEAD state")

It means that further commits will take the commit you checked out as their parent, and eventually form a commit chain, but this chain isn't referenced by anything else than your current HEAD.

Now, when you check out something else, branch or commit, the commit (or commit chain) is left "dangling", waiting for the garbage collector, depending on settings, but in most cases not under 90 days, so no panic.

How to get back on track

But well before garbage collection happens, you can get it back by just checking it again (out). Either by just looking up in your terminal output, where the commit hash is likely to have been displayed by a number of git commands (quick and easy when possible!), either, like already suggested, in your reflog which contains a history of your previous HEAD positions.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
-1

This late answer is intended for anyone has the same issue.

The OP's problem is the detached headhttp://git-scm.com/docs/git-checkout#_detached_head

His exact problem is at commit e and commit f in the article

Tri
  • 199
  • 9