One of the best things about Git is the ability to retrieve and view a full history of every commit you’ve made to a project. It’s very powerful as regardless of the direction you take or any future commits you make you’ve always got access to a chronological history of commits.

Sometimes however when presenting, delivering or cloning a project for simplicity and cleanliness you may not want a history of development branches or commits to be visible, showing only the latest version of code.

Flattening your Git Repository

The first step is to remove the .git folder which contains all the git internals for the project. This won’t affect the project files, however it will remove all traces of git and it’s history/configuration for this project.

cd your_project_dir

To remove the .git folder on a Linux machine type rm -rf .git or on Windows rd /s /q .git.

Once this folder is removed, your project no longer contains any Git information and we will reinitialise git from scratch.

git init
git add .
git commit -m 'Commit Message'
git remote add origin https://your_git_repo_url

At this point we have a fresh git project with all of your original project files, and no file or branch history. The next step is to push it to your selected repo.

git push --force

The force command ignores the warnings and the current state of your local project in relation to the remote repo and forces the push of your local repo, overwriting everything upstream.

A word of warning. Be very careful with this and any command that uses forced pushing. Only use this if you are confident with Git commands and are able to revert in the case of an error.