65.9K
CodeProject is changing. Read more.
Home

Git repository cleanup

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Apr 2, 2013

CPOL
viewsIcon

11311

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.