
How to delete all commit history in git/github
Deleting the .git folder may cause problems in your git repository. If you want to delete all your commit history but keep the code in its current state, it is very safe to do it as in the following:
- Checkout
git checkout --orphan latest_branch
- Add all the files
git add -A
- Commit the changes
git commit -am "commit message"
- Delete the branch
git branch -D main
- Rename the current branch to main
git branch -m main
- Finally, force update your repository
git push -f origin main
Sometimes you may need to create a brand new branch on a git project that doesn't contain anything present on the other branches nor shares an history with them, called orphan branch.
This snippet describes how to do it:
BRANCH_NAME=$1
git checkout --orphan $BRANCH_NAME
git rm -rf .
rm .gitignore
echo "# $BRANCH_NAME" > README.md
git add README.md
git commit -m "Initial commit for branch $BRANCH_NAME"
git push origin $BRANCH_NAME
This snippet can be used as a sequence of commands, replacing $1 with any value for the branch name, or in a bash script, for example naming it orphan_branch.sh and running it with sh ./orphan_branch.sh new_branch_name.
for more info about --orphan: https://git-scm.com/docs/git-checkout
Related articles
Mastering Git Bisect: A Developer's Guide to Efficient Debugging
Dive deep into the world of Git Bisect to uncover how this powerful tool can revolutionize your debugging process, making it more efficient and less time-consuming.
Git Stash
Git stash is a built-in command with the distributed Version control tool in Git that locally stores all the most recent changes in a workspace and resets the state of the workspace to the prior commit state.
Git Branching
Branching is a feature available in most modern version control systems.