Git Notes - Submodule && detached HEAD

When I forget to add submodule

I moved my media directory to the project as well, since it's a git project I have to add it as a git submodule add, however I forgot to do so and did git add . instead, so on my earlier commit, the folder is shown as a text on Github.

Solution1:

  1. Unstage it

    1
    $ git rm --cached media
    
  2. Add it in a correct way

    1
    $ git submodule add url_to_repo media
    

Detached HEAD

My git in media directory somehow got a detached state:

1
2
$ git commit -m "aaa"
HEAD detached from 8799335

To fix that2 and merge the untracked changes, so I need to checkout the corresponding branch:

1
$ git checkout master

Then it shows the log:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
Warning: you are leaving 4 commits behind, not connected to
any of your branches:

  69c7986 yEd try
  7f5f6d2 new pics
  eb3656a new pic
  19a5966 2 new pics

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

git branch <new-branch-name> 69c7986

Switched to branch 'master'

Then follow the instruction and push:

1
2
$ git branch tmp 69c7986
$ git push origin master