Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Git repository cleanup

0.00/5 (No votes)
2 Apr 2013 1  
Splitting repository and remove folders preserving history

Introduction

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

Background

My method comes from severals sources throught internet, but mainly from these articles: www.martinahrer.at and stackoverflow.com

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:  

cd PATH_SECONDARY_REPO
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. 

History

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here