Completely Flattening a Git Repository

Whilst working on a recent project, I ended up with a massive git repository due to frequent commits of large-ish files. As I was the only one working on the project and I was certain I didn’t need anything from the history, I wanted to be able to flatten all the commits in the repository.

I didn’t find exactly what I was after but found a handy tip on Stack Overflow. In essence you re-initialize your local repository and force a push to your remote. A word of warning, if you have anyone else pulling or pushing to the remote repository, they will hate you for this as it will completely screw up any revision history or branches they may be working on.

Here’s my modified version of the tip on Stack Overflow to take into account .gitignore and removing the existing .git repository:


$ cd {{project-directory}}
$ rm -rf .git
$ git init
$ git add .
$ git commit -m 'Initial commit'
$ git remote add origin {{url}}
$ git push --force

p.s. Don’t say I didn’t warn you!

Published by

Jamie

Hi, I am the Managing Director of SonicIQ Limited in the UK. I have been working in the web development industry since 1999 and have been running SonicIQ since 2001. Currently Ruby On Rails is my preferred development platform. I am experienced in designing with web standards, HTML5, CSS3 and Javascript.

6 thoughts on “Completely Flattening a Git Repository”

  1. I also think git has the option to remove specific commits out (which is handy if you want trim down things without loosing recent commit history). Not sure how to do it, I just seem to recall one of the chaps mentioning it at our local Ruby groups.

  2. @Brian I didn’t have a lot of luck completely flattening a repo with rebase and so found this the simplest method. Thanks for the link, I’ll have have a read and give rebase another shot.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>