Git Clear your History

Page content

Clear History

have you ever checked in some binarys, confidential stuff or something else by mistake ? Git will keep all your history, that’s their design and purpose.

how ever, if you need to cleanup once, here is a short tutorial.

Kill Git Config

cd myrepo
cat .git/config -> note down the url
url=$(git config --get remote.origin.url)
rm -rf .git

Create New Repo

git init
git add .
git commit -m "Removed history, ..."

Push Remote

git remote add origin git@host/yourrepo  <- URL you noted down above
git remote add origin $url
git push -u --force origin master

All in One

_url=$(git remote -v |awk '/fetch/ { print $2 }')
rm -rf .git
git init
git add .
git commit -m "Removed history ..."
git remote add origin ${_url}
git push -u --force origin main
unset _url

and you’re done :)

Reset Repo

you may wanna reset your local repo fetch from origin ? and clean local, untracked filed ?

git fetch origin
git reset --hard origin/main
git clean -d -f

some more Git commands


Any Comments ?

sha256: 84ab3cab24b8b100e86fadd05318a869ca3c7a98c069d8a0147b9f5f9df53fd6