0

Git is supposed to be a tool for easily switching back to test things and then getting back to what you were doing. Sometimes I switch back to an old commit by doing

git checkout old_commit_id

then to get back I do

git checkout latest_commit_I_did

and start doing work. Then I commit this work.

git commit -m "did some work"

Then I go back to another commit with

git checkout another_old_commit_id

Then my commit did some work is simply LOST forever. Why it's so easy to lose a ton of work on git? Since git checkout latest_commit_I_did is the last commit I did, why it simply not added my commit on top of this one?

I think it has something to do with it not knowing in which branch I am but it should, at least warn me. Why simply delete everything I did with no warnings? I've already lost some important works because of this.

phd
  • 82,685
  • 13
  • 120
  • 165
Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
  • 1
    1. Can you clarify why you are switching between commits? 2. You didn't lose anything, it's all accessible in the reflog: https://git-scm.com/docs/git-reflog – JBallin Jul 05 '20 at 06:16
  • @JBallin I'm switching just to see how the code was before and then go back – Guerlando OCs Jul 05 '20 at 06:30
  • @JBallin I found this `bc492f1 HEAD@{17}: commit: backup` on doing `git reflow show`. I think it's the commit I lost, how do I recover? – Guerlando OCs Jul 05 '20 at 06:33
  • 1
    Your new commit is still there, not lost. Either `git checkout your_branch` if the commit was made on one or `git checkout your_latest_commit_id` which would always work. – VLAZ Jul 05 '20 at 06:34
  • ok, I fixed by ding `git reset --hard id_found_on_git_reflow` but I do not have any idea of whats going on. The commit that I lost was made when I was on `latest_commit_id`. Then I've gone to `old_random_commit` then did `git checkout master` and the commit was gone. Why this happens? – Guerlando OCs Jul 05 '20 at 07:35
  • https://stackoverflow.com/search?q=%5Bgit%5D+recover+detached+HEAD+commits – phd Jul 05 '20 at 07:39

1 Answers1

0

The commit isn't lost, it just takes you back in time to the previous commit. To come back to the latest commit just enter git checkout master or the name of your branch and you will be back to your latest commit.

Nishith Savla
  • 310
  • 2
  • 10
  • this is not what happens. If I checkout back to the latest commit (NOT `git checkout master`, but instead `git checkout latest_commit`) and edit something and `git commit -m "this commit will go away"`, then if I `git checkout random_old_commit` and then `git checkout master`, then `this commit will go away` really gone away – Guerlando OCs Jul 05 '20 at 07:34
  • Try checking back to the branch instead of checking out to the latest commit – Nishith Savla Jul 05 '20 at 09:08