Click here to Skip to main content
15,115,146 members
Articles / DevOps / Git
Tip/Trick
Posted 2 Apr 2013

Tagged as

Stats

10.4K views
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

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)

Share

About the Author

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

Comments and Discussions

 
-- There are no messages in this forum --