Click here to Skip to main content
15,885,366 members
Articles / DevOps / Git
Tip/Trick

Git repository cleanup

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
2 Apr 2013CPOL 11.2K   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, 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 --