I did a git checkout 5c24d6d and lost my commits beyond this point. Then I tried to get back to my old state. To do so I executed below steps. I understand checkout is normally used to switch between branching. Can someone please explain what happened with the steps I followed. With my 1st checkout command, did git create a temporary branch (called HEAD detached at 5c24d6d)? What happened when I checked out origin? Did it create a new temporary branch again and removed the old temporary branch? Lastly, when I switched to master did git removed the temporary branch?
To experiment further: I did a git checkout 5c24d6d and then switched to master using git checkout master, it had the same result.
I was on master branch and did a git checkout 5c24d6d. This changed my working copy to the point of 5c24d6d. It also said that, I am in a detached HEAD state. Then when I did git log I can see logs till 5c24d6d but not the ones done after. At this point I looked at the branch and could see:
* (HEAD detached at 5c24d6d)
master
As an attempt to recover, I executed git checkout origin. This brought me back to the last commit having SHA1, 8c584c3. At this point the branch looked like this:
* (HEAD detached at origin/master)
master
Then I switched to branch master using, git checkout master and checked my branch using git branch. Now I can only see:
* master
The branch with head details is gone.
Below are my steps in details:
$ git log --oneline -10
8c584c3 (HEAD -> master, origin/master, origin/HEAD) deleting test
c4fcf55 adding line 3
fdd4670 adding line 2
8504aa4 adding test
be91837 Deleting .gitignore files from projects
5c24d6d Merge branch 'master' of https://github.com/siddswork/gradle
c80ace7 Creating lib an bin directories inside run with empty .gitignore
e91516d Updating README.md
a54a193 Create README.md
89b60ec Adding makefiles
$ git branch
* master
$ git checkout 5c24d6d
Note: checking out '5c24d6d'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 5c24d6d Merge branch 'master' of https://github.com/siddswork/gradle
$ git log --oneline -3
5c24d6d (HEAD) Merge branch 'master' of https://github.com/siddswork/gradle
c80ace7 Creating lib an bin directories inside run with empty .gitignore
e91516d Updating README.md
$ git branch
* (HEAD detached at 5c24d6d)
master
$ git checkout origin
Previous HEAD position was 5c24d6d Merge branch 'master' of https://github.com/siddswork/gradle
HEAD is now at 8c584c3 deleting test
$ git branch
* (HEAD detached at origin/master)
master
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git branch
* master
$ git log --oneline -10
8c584c3 (HEAD -> master, origin/master, origin/HEAD) deleting test
c4fcf55 adding line 3
fdd4670 adding line 2
8504aa4 adding test
be91837 Deleting .gitignore files from projects
5c24d6d Merge branch 'master' of https://github.com/siddswork/gradle
c80ace7 Creating lib an bin directories inside run with empty .gitignore
e91516d Updating README.md
a54a193 Create README.md
89b60ec Adding makefiles