Click here to Skip to main content
15,567,415 members
Articles / DevOps / Git
Posted 2 Apr 2013

Tagged as


1 bookmarked

Git repository cleanup

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Apr 2013CPOL
Splitting repository and remove folders preserving history


That was the second time that I need to do that, so I'll merge my finds into this tip. 


My method comes from severals sources throught internet, but mainly from these articles: and

The "cooking" commands to isolate a folder 

Clone main repository:  

git clone --no-hardlinks PATH_MAIN_REPO PATH_SECONDARY_REPO   

Remove link from cloned repository:  

git remote rm origin

Isolating desired folder: 

git filter-branch --subdirectory-filter FOLDER_TO_BECOME_ROOT 

Now all files from that folder are "moved" to root (something like that).
I don't know if its a bug or not, but GitExtensions shows 2 'master' branches, but only filtered is accessible.

Command to remove a folder from history  

Remove folders from repository's history: 

git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch FOLDER_THAT_BECAME_ROOT" --prune-empty -- --all  

Cleanup empty entries:

git rev-list --remove-empty HEAD
git gc --prune=now --aggressive 

After that, your repository won't have any entry of removed folder. 


  • 2 Apr 2012: Start of tip. 
  • 2 Apr 2012: Adding switches to avoid empty enties and cleaning history. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) ThyssenKrupp Industrial Solutions
Brazil Brazil
I'm just another ordinary programmer.

Comments and Discussions

-- There are no messages in this forum --