git,

About deleting your Git repo commit history

Oct 04, 2022 · 1 min read · Post a comment

Removing things has been my favorite topic to write about. Pushing any sensitive information as part of a Git commit or starting from scratch could be arguably enough reasons to remove all Git commit history. Here are two solutions you can try.

Prerequisites

  • Git

Solution

#1 Using an orphaned branch

Orphaned branches are Git branches without parents = w/o Git history.

Step 1. Checkout an orphaned branch.

git checkout --orphaned <new_branch>

Step 2. Stage all files required for versioning. For instance:

git add -A

Step 3. Commit changes.

git commit -m "init commit"

Step 4. Remove the main branch.

git branch -D main

Step 5. Rename the current branch your HEAD points to.

git branch -m main

More about renaming branches.

Step 6. Finally, push changes to remote origin.

git push -f origin main

#2 Delete the whole Git history

Probably the easiest and laziest method to do so.

Step 1. Remove your local Git repository.

cd <your_repo_dir> && rm -rf .git

More about properly deleting Git repos.

Step 2. Initialize a new repo, stage files and commit changes.

git init
git add .
git commit -m "reinit repo" 

Step 3. Add remote origin and push to remote.

git remote add origin [email protected]:username/repo_name
git push -f origin main

Conclusion

To find more neat Git commands and hacks, simply browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.

git