471 words
2 minutes
How to detach a subdirectory to a new git repository with full history
2025-06-06
统计加载中...

Instructions#

  1. cd to the top-level directory of the project or open bash here

    Terminal window
    cd <path/to/main-repo-folder>
  2. [Main Repo] Split the target folder into a temporary branch

    Terminal window
    git subtree split -P <path/to/target-folder> -b <name-of-temp-branch>
  3. [Main Repo] Recursively remove the target folder

    Terminal window
    git rm -rf <path/to/target-folder>
  4. [Main Repo] Create a new repository folder (Use the previously deleted folder path if setting it up as a submodule)

    Terminal window
    mkdir -pv <path/to/new-repo-folder>
  5. [Main Repo] Navigate to the new repository folder

    Terminal window
    cd <path/to/new-repo-folder>
  6. [New Repo] Initialize the repository

    Terminal window
    git init
  7. [New Repo] Pull the temporary branch

    Terminal window
    git pull <path/to/main-repo-folder> <name-of-temp-branch>
  8. [New Repo] (Optional) Check if any commit msg needs to be reworded

    Terminal window
    git rebase -i --root
  9. [New Repo] Add the remote origin

    Terminal window
    git remote add origin <url-of-new-repo>
  10. [New Repo] Push local changes to the remote repository

    Terminal window
    git push -u origin master
  11. [New Repo] Navigate back to the main repository folder

    Terminal window
    cd <path/to/main-repo-folder>
  12. [Main Repo] Delete the temporary branch

    Terminal window
    git branch -D <name-of-temp-branch>
  13. [Main Repo] (Optional) Add the independent repository as a submodule

    Terminal window
    git submodule add <url-of-new-repo> <path/to/new-repo-folder>

Branch#

Terminal window
Spr_Aachen@DESKTOP-UEUQRBG MINGW64 /d/Projekt/Python Projects/Easy-Voice-Toolkit (dev)
$ git subtree split -P server -b temp
Created branch 'temp'
4b36120869a031f38120ac71b0264f5545886471
Spr_Aachen@DESKTOP-UEUQRBG MINGW64 /d/Projekt/Python Projects/Easy-Voice-Toolkit (dev)
$ git rm -rf server
Migrating git directory of 'server/AudioProcessor' from
'D:/Projekt/Python Projects/Easy-Voice-Toolkit/server/AudioProcessor/.git' to
'D:/Projekt/Python Projects/Easy-Voice-Toolkit/.git/modules/AudioProcessor'
Migrating git directory of 'server/GPT_SoVITS' from
'D:/Projekt/Python Projects/Easy-Voice-Toolkit/server/GPT_SoVITS/.git' to
'D:/Projekt/Python Projects/Easy-Voice-Toolkit/.git/modules/GPT-SoVITS'
Migrating git directory of 'server/VPR' from
'D:/Projekt/Python Projects/Easy-Voice-Toolkit/server/VPR/.git' to
'D:/Projekt/Python Projects/Easy-Voice-Toolkit/.git/modules/VPR'
Migrating git directory of 'server/Whisper' from
'D:/Projekt/Python Projects/Easy-Voice-Toolkit/server/Whisper/.git' to
'D:/Projekt/Python Projects/Easy-Voice-Toolkit/.git/modules/Whisper'
rm 'server/AudioProcessor'
rm 'server/GPT_SoVITS'
rm 'server/VPR'
rm 'server/Whisper'
rm 'server/main.py'
rm 'server/routers.py'
rm 'server/tools.py'
rm 'server/utils/__init__.py'
rm 'server/utils/logger.py'
Spr_Aachen@DESKTOP-UEUQRBG MINGW64 /d/Projekt/Python Projects/Easy-Voice-Toolkit (dev)
$ mkdir -pv server
Spr_Aachen@DESKTOP-UEUQRBG MINGW64 /d/Projekt/Python Projects/Easy-Voice-Toolkit (dev)
$ cd server
Spr_Aachen@DESKTOP-UEUQRBG MINGW64 /d/Projekt/Python Projects/Easy-Voice-Toolkit/server (dev)
$ git init
Initialized empty Git repository in D:/Projekt/Python Projects/Easy-Voice-Toolkit/server/.git/
Spr_Aachen@DESKTOP-UEUQRBG MINGW64 /d/Projekt/Python Projects/Easy-Voice-Toolkit/server (master)
$ git pull "D:/Projekt/Python Projects/Easy-Voice-Toolkit" temp
remote: Enumerating objects: 32, done.
remote: Counting objects: 100% (32/32), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 32 (delta 17), reused 15 (delta 13), pack-reused 0 (from 0)
Unpacking objects: 100% (32/32), 7.12 KiB | 117.00 KiB/s, done.
From D:/Projekt/Python Projects/Easy-Voice-Toolkit
* branch temp -> FETCH_HEAD
Spr_Aachen@DESKTOP-UEUQRBG MINGW64 /d/Projekt/Python Projects/Easy-Voice-Toolkit/server (master)
$ git remote add origin https://github.com/Spr-Aachen/EasyVoiceToolkit-Backend.git
Spr_Aachen@DESKTOP-UEUQRBG MINGW64 /d/Projekt/Python Projects/Easy-Voice-Toolkit/server (master)
$ git push -u origin master
Enumerating objects: 32, done.
Counting objects: 100% (32/32), done.
Delta compression using up to 16 threads
Compressing objects: 100% (31/31), done.
Writing objects: 100% (32/32), 6.73 KiB | 1.35 MiB/s, done.
Total 32 (delta 17), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (17/17), done.
To https://github.com/Spr-Aachen/EasyVoiceToolkit-Backend.git
* [new branch] master -> master
branch 'master' set up to track 'origin/master'.
Spr_Aachen@DESKTOP-UEUQRBG MINGW64 /d/Projekt/Python Projects/Easy-Voice-Toolkit/server (master)
$ cd "D:/Projekt/Python Projects/Easy-Voice-Toolkit"
Spr_Aachen@DESKTOP-UEUQRBG MINGW64 /d/Projekt/Python Projects/Easy-Voice-Toolkit (dev)
$ git branch -D temp
Deleted branch temp (was 4b36120).
Spr_Aachen@DESKTOP-UEUQRBG MINGW64 /d/Projekt/Python Projects/Easy-Voice-Toolkit (dev)
$ git submodule add https://github.com/Spr-Aachen/EasyVoiceToolkit-Backend.git server
Adding existing repo at 'server' to the index
warning: in the working copy of '.gitmodules', LF will be replaced by CRLF the next time Git touches it
How to detach a subdirectory to a new git repository with full history
https://blog.spr-aachen.com/posts/tutorial-git/detachsubdirectorytorepo/
Author
Spr-Aachen
Published at
2025-06-06
License
CC BY-NC-SA 4.0

Some information may be outdated

封面
Music
Artist
封面
Music
Artist
0:00 / 0:00