move a folder to other git repo

To move a sub-directory/folder to other git repo. Let's leave the usual working directory as is, and create new directory for this experiments.

Go to new directory, let's say ~/parallel-universe.

sun/
  fossil/
      dinousar
      tardigrades
  chemicals/
  energy/
  .git/
earth/

Steps:

  cd ~/parallel-universe/
  git clone [email protected]:universe/sun.git
  cd sun/ 
  git remote rm origin
  git filter-branch --subdirectory-filter fossil -- --all
  #Folder `sun` should now contain only `.git` and `fossil/`
  
  cd ~/parallel-universe/
  mv sun/ earth/
  cd earth/
  git remote add origin [email protected]:universe/earth.git
  git pull origin master --allow-unrelated-histories
  git push origin master

Now you can remove the folder fossil from sun if want to.