Remove pushed git commit history

Remove pushed git commit history

How to remove history from github

Incase you pushed sensitive data or wrong files into your github repository and need to remove it completely. I want to outline the steps I took to do the same. Most of the answers online only reverts the commit but don't help to remove the history and record completely from the branch.

Instructions

See number of commits on the log

git rev-list HEAD --count

Create a new branch, your branch switches from master at this point.
git checkout --orphan [branch-name], in this example, I used resolve_branch

git checkout --orphan resolve_branch

Add all counts into this branch

git add -A

Commit to the branch

git commit -am "Remove Previous Commits"

Delete master branch

git branch -D master or main

Rename created sub-branch to main, in this example resolve_branch to main

git branch -m main

Git push new main to origin main (needs internet)

git push -f origin main

Set upstream for tracking

git branch --set-upstream-to=origin/main main

Run git pull command, should be up to date

git pull

Hope you find this useful