user2136963
user2136963

Reputation: 2606

Why did sourcetree delete my commit?

I'm learning to use source control via git and sourcetree

My code commits graph had only one branch.

Sourcetree was drawing label "Master" on the most recent commit called "Cells verification"

I added a line of comment to PlayerDispatcher file. //commit undo test

I commited it like "Commit undo test"

Then I double clicked the previous version (double click is for checkout). There appeared a warning saying that a separate HEAD will be created. I din't understand what is wrong with it and clicked OK.

After it the project had two branches. On "Cells verification" there was a label "HEAD". On "Commit undo test" there was a label "Master"

I deleted SentCell file and added changes to a PlayerDispatcher file.

After it I realized that I need some functionality of deleted SentCell back, but in other file.

I commited all changes like "Changed PlayerDispatcher and deleted SentCell".

Then I double-clicked "Cells verification". Now "Cells verification" is labeled head "Commit undo test" is labeled "Master" and "Changed PlayerDispatcher and deleted SentCell" does not exist (PlayerDispatcher change are lost).

Graph has only one branch

Why did checkout delete newer file? Is there a way to restore it (I did changes mostly for testing, but I'm still interested in possibility of it)? Is there a way to prevent git from any permanent deletions?

Upvotes: 2

Views: 583

Answers (1)

Chris
Chris

Reputation: 136918

Then I double clicked the previous version (double click is for checkout). There appeared a warning saying that a separate HEAD will be created. I din't understand what is wrong with it and clicked OK.

After it the project had two branches. On "Cells verification" there was a label "HEAD". On "Commit undo test" there was a label "Master"

It might have looked like you had two branches, but you didn't really. HEAD isn't a branch; it's a pointer to what's currently checked out. When you checked master out again, HEAD moved to the same place as master, leaving nothing pointing to your new commit.

Git only shows commits that are reachable from a head. Because you didn't create a second branch, Git isn't showing the commit that is there, but it's probably still in your repository.

Try running git reflog. This should include something like

abcd123 HEAD@{0}: checkout moving from <some long hash> to master

If you haven't modified your repository since, this will likely be the first line. If you have modified it, you may have to go down a few lines. In this case, the HEAD@ number will also be different.

Once you find the right line, copy the hash (probably the first 7 characters or so should be enough) and check it out again:

git checkout <hash from reflog>

You should find that you've got your commit back. Now, if you want to keep it, create a branch to point to it:

git branch <branch-name> <hash from reflog>

Upvotes: 4

Related Questions