Skip to content

git 💥

Ben Forbes Griffith edited this page Oct 9, 2023 · 2 revisions

Take off and nuke the entire git history from orbit… 💣 “It’s the only way to be sure!” 💥

  1. remove all git history locally

rm -rf .git

  1. create a new local repo

git init

  1. add everything

git add .

git commit -m "initial commit"

  1. nuke history on GitHub (irreversible!) 💥

git remote add origin git@github.com:USERNAME/REPOSITORY.git

git push -u --force origin master


  • add a git ci --allow-empty as first commit to a new repo so there is always the option to go back to the empty state? 💭

little orphan Annie branch it…

The first commit made on an orphan branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits…

Ergo, this can be useful when you want to publish the tree from a commit without exposing its full history. You might want to do this to publish an open source branch of a project whose current tree is “clean” (but whose full history contains proprietary or otherwise encumbered bits of code).

  • Regardless, the strategy is: after you’ve created the orphan branch, you swap it out the with the “old” master:
  1. Create a new branch, with no parent commits

git checkout --orphan squashed-master master

git commit -m "prime commit"

  1. overwrite the old master branch with the new one…

git branch -M squashed-master master


commit-tree method

  • The git commit-tree creates a new commit object based on the provided tree object and emits the new commit object id on stdout
  • The command below will create one commit object using git commit-tree and then reset the HEAD to that commit:

git reset $(git commit-tree HEAD^{tree} -m "🎉 First commit")


Alias it!

  • Since the git commit-tree method is a git one-liner, you can alias it:

git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} -m "${1:-prime commit}");};f'

  • Thus, from thence on, you can just run git squash-all in any repo you please:

git squash-all

  • Also check out this alias within ./freshinstall, an opinionated tool to automatically configure 🍏 MacOS (preferences, dotfiles, installed software, etc.)

pushing an “altered history” branch:

  • After rewriting history, you’ll need to use --force when pushing.

git push --force origin master…


nuke the history of a GitHub Wiki so that only the most recent revision of the Wiki’s content is available

# clone the Wiki…
git clone https://github.com/[user]/[repo].wiki.git
# remove the .git folder
rm -rf .git
# reconstruct the local repo with only latest content
git init
git add .
git commit -m "Initial commit"
# force push to GitHub
git remote add origin <github-uri>
git push -u --force origin master