Search code examples
gitgit-branch

git commits were lost - what have I done wrong?


I have a local git repository. I wanted to try some changes out so I branched and tested. Then I didn't like the test so I deleted the branch. Then I found my whole commits were gone except for the first one.

Here are the git commands I used.

git checkout -b testing

Some files were edited with several commits on the testing branch.

git commit -a

Then I didn't like the changes and wanted to discard the changes.

git checkout master
git branch -d testing

At this point there was a warning saying that the testing branch was not fully merged. so I did

git branch -D testing

Then all of my commits on master are gone.

What was wrong and is it possible to recover?


Solution

  • They can be recovered through the reflog

    Try git reflog and look for the commits and try checking out directly to them, that will let you get them back.

    This is as expected. You deleted the branch you made commits to, why would the commits not be gone?

    You committed NOTHING to master, hence master is unchanged.

    You might also want to consider starting by reading this http://git-scm.com/book as you seem confused as to how git works.

    Again in summary: Nothing is wrong. You never comitted to master, you committed to your branch. Git then warned you that you would lose those commits since they weren't pushed anywhere and you ignored the warning.