Skip to content

Instantly share code, notes, and snippets.

@bhdicaire
Created March 26, 2022 18:43
Show Gist options
  • Save bhdicaire/bdd5ab4877605bd06ee680428b61664f to your computer and use it in GitHub Desktop.
Save bhdicaire/bdd5ab4877605bd06ee680428b61664f to your computer and use it in GitHub Desktop.

Fresh start for a git repository

The commit history doesn’t make sense, and disk space is ridiculous for a project with a lot of moving parts. It’s time to start over and enforce commit message that reflect the underlying change in a short, precise manner.

It's time to nuke everything except current code.

Easy way

cd git-repository
du -sh .git
cp .git/config .
rm -rf .git
git init
cp config .git
git add .
git commit -m "Initial commit after deleting the history"
git push -u --force origin master // or git push --mirror --force
du -sh .git

Safer way according to Stack Overflow

git checkout —orphan newBranch
git add -A  # Add all files and commit them
git commit -m ‘Clear history’
git branch -D master  # Deletes the master branch
git branch -m master  # Rename the current branch to master
git push -f origin master  # Force push master branch to github
git gc —aggressive —prune=all     # remove the old files

Source: Make the current commit the only (initial) commit in a Git repository

Giving up and cloning the remote repository

Let’s get back to the state of the remote repository, This will clone the master main branch by default, use git clone -b branch-name for something specific.

sudo rm -r git-repository
git clone https://github.com/bhdicaire/git-repository.git
cd git-repository
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment