Git: Detached HEAD

CommandNew filesChanged filesDeleted files
Alice/> git init --template=""
Initialized empty Git repository in /home/rene/github/github/git-internals/repos/detached-HEAD/Alice/.git/
snap no: 1
.git/HEAD
.git/config
Alice/> touch foo
snap no: 2
Alice/> git add foo
snap no: 3
Alice/> git commit foo -m "add foo" 
[master (root-commit) 3fd0cb8] add foo
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 foo
snap no: 4
.git/COMMIT_EDITMSG
.git/logs/HEAD
.git/logs/refs/heads/master
.git/objects/3f/d0cb8a8cfefd32fcbb67f15fd561a8ce63fcf1 [commit]
.git/objects/4d/5fcadc293a348e88f777dc0920f11e7d71441c [tree]
.git/refs/heads/master
Alice/> touch bar
snap no: 5
Alice/> git add bar
snap no: 6
.git/index
Alice/> git commit bar -m "add bar" 
[master 7492cd8] add bar
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bar
snap no: 7
.git/COMMIT_EDITMSG
.git/logs/HEAD
.git/logs/refs/heads/master
.git/refs/heads/master
Alice/> git checkout 3fd0cb8
Note: checking out '3fd0cb8'.

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 3fd0cb8... add foo
snap no: 8
.git/HEAD
.git/index
.git/logs/HEAD
Alice/> ls -l
total 0
-rw-rw-r-- 1 rene rene 0 Jan  1 00:07 foo
snap no: 9
Alice/> git branch
* (HEAD detached at 3fd0cb8)
  master
snap no: 10
Alice/> touch baz
snap no: 11
Alice/> git add baz
snap no: 12
.git/index
Alice/> git commit baz -m "add baz" 
[detached HEAD 35855da] add baz
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 baz
snap no: 13
.git/COMMIT_EDITMSG
.git/HEAD
.git/logs/HEAD
Alice/> git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  35855da add baz

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 35855da

Switched to branch 'master'
snap no: 14
.git/HEAD
.git/index
.git/logs/HEAD
Alice/> ls -l
total 0
-rw-rw-r-- 1 rene rene 0 Jan  1 00:13 bar
-rw-rw-r-- 1 rene rene 0 Jan  1 00:13 foo
snap no: 15
Alice/> git merge 35855da
Merge made by the 'recursive' strategy.
 baz | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 baz
snap no: 16
.git/index
.git/logs/HEAD
.git/logs/refs/heads/master
.git/refs/heads/master
Alice/> ls -l
total 0
-rw-rw-r-- 1 rene rene 0 Jan  1 00:15 bar
-rw-rw-r-- 1 rene rene 0 Jan  1 00:15 baz
-rw-rw-r-- 1 rene rene 0 Jan  1 00:15 foo
snap no: 17