Detach/move subdirectory into separate git repo

0 votes
I have a git repo which consists of many subdirectories. I recently found out that one of the subdirectories is not relevant and now I’m trying to remove it from the repo. How do I go about this?
Aug 17, 2018 in Git & GitHub by Hannah
• 14,080 points
35 views

2 answers to this question.

0 votes

Clone your repo first and the use git filter-branch to mark everything but the subdirectory that you want in your new repo to be garbage-collected.

Step 1 - To clone your local repository:

         git clone /XYZ /ABC

step 2 - Let us preserve the interesting branches which we want to rewrite as well, and then remove the origin to avoid pushing there and to make sure that old commits will not be referenced by the origin:

cd /ABC
for i in branch1 br2 br3; do git branch -t $i origin/$i; done
git remote rm origin

or for all remote branches:

cd /ABC
for i in $(git branch -r | sed "s/.*origin\///"); do git branch -t $i origin/$i; done
git remote rm origin

step 3 - Then use filter-branch and reset to exclude the other files, so they can be pruned

git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter ABC HEAD

step 4 - Then delete the backup reflogs so the space can be truly reclaimed (although now the operation is destructive)

git reset --hard
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
git reflog expire --expire=now --all
git gc --aggressive --prune=now
answered Aug 17, 2018 by Kalgi
• 37,320 points
0 votes
git filter-branch --prune-empty --subdirectory-filter <YOUR_SUBDIR_TO_KEEP> master
git push <MY_NEW_REMOTE_URL> -f .

this will remove the subdirectories you don't want and then push to the new remote.

answered Aug 20, 2018 by Nilesh
• 6,900 points

Related Questions In Git & GitHub

0 votes
1 answer
0 votes
1 answer

How to do a re-merge into another branch in git

Seems like you want to rebase your ...READ MORE

answered May 7, 2018 in Git & GitHub by DareDev
• 6,810 points
95 views
0 votes
1 answer

Git origin not found

For this to work, you need to ...READ MORE

answered Apr 25, 2018 in Git & GitHub by DragonLord999
• 8,380 points
86 views
0 votes
1 answer

Configure Git and GitHub

In version control with Git, we deal ...READ MORE

answered Apr 26, 2018 in Git & GitHub by DragonLord999
• 8,380 points
17 views
+13 votes
2 answers
0 votes
1 answer
+1 vote
2 answers

Git Commands

This command basically adds all your changes ...READ MORE

answered Aug 14, 2018 in Git & GitHub by Kalgi
• 37,320 points
45 views
0 votes
2 answers

difference between git remote and git clone

git remote add just creates an entry in ...READ MORE

answered Aug 7, 2018 in Git & GitHub by Kalgi
• 37,320 points
419 views