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
• 18,520 points
827 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
• 52,350 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
• 7,060 points

Related Questions In Git & GitHub

–1 vote
1 answer

Having problem in Github and remote Git repo with production and development code

I think you could start with this. The ...READ MORE

answered Apr 10, 2018 in Git & GitHub by shubham
• 7,340 points

edited Dec 15, 2023 by Khan Sarfaraz 1,117 views
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,890 points
2,456 views
0 votes
1 answer

How to create branch in GitHub from my local Git repo?

Hi@akhtar, To sync your existing branch in GitHub, ...READ MORE

answered May 7, 2020 in Git & GitHub by MD
• 95,460 points
885 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,034 views
+2 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
• 52,350 points
1,038 views
0 votes
3 answers

difference between git remote and git clone

HI.... GIT REMOTE add just creates an entry in ...READ MORE

answered Jul 11, 2020 in Git & GitHub by anonymous
23,435 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP