Skip to content

Instantly share code, notes, and snippets.

@yaredc
Created September 8, 2019 11:51
Show Gist options
  • Save yaredc/38dc9243c2f7f4cf7082f2fa7732af5f to your computer and use it in GitHub Desktop.
Save yaredc/38dc9243c2f7f4cf7082f2fa7732af5f to your computer and use it in GitHub Desktop.
Delete/reset history in GIT repository.

RESET (EFFECTIVELY DELETE) TARGETED BRANCH HISTORY

  1. Checkout tmp branch without history, from whatever targeted commit or branch you want
  2. Delete targeted branch
  3. Rename tmp branch to targeted branch
  4. Force push to repo

IN CASE OF master BRANCH

git checkout --orphan tmp #KEY STEP IS TO CHECKOUT AS ORPHAN
git add -A
git commit -am "init"
git branch -D master
git branch -M master
git push -f origin master

IN GENERAL

#!/bin/sh

TARGET='master'

git checkout "$TARGET"
git checkout --orphan tmp
git add -A
git commit -am "init"
git branch -D "$TARGET"
git branch -M "$TARGET"
git push -f origin "$TARGET"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment