Skip to content

Instantly share code, notes, and snippets.

@rfuehrer
Last active February 25, 2020 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfuehrer/8216c2dd8fc8c333198f4a9526102385 to your computer and use it in GitHub Desktop.
Save rfuehrer/8216c2dd8fc8c333198f4a9526102385 to your computer and use it in GitHub Desktop.
Bash script to remove history (commits & events) from remote github repository
#!/bin/bash
DEFAULT_GITREPO=<repo-name>
DEFAULT_GITACCOUNT=<user/org-name>
DEFAULT_GITRIGHT_DELETE=
DEFAULT_GITRIGHT_CREATE=
CURRDIR=$(pwd)
echo ""
echo "#####################################################################"
echo "### "
echo "### Currect directoy : $CURRDIR"
read -p "### Git Repository [$DEFAULT_GITREPO]: " GITREPO
if [[ -z $GITREPO ]]; then
GITREPO=$DEFAULT_GITREPO
fi
read -p "### Git Organization/Username [$DEFAULT_GITACCOUNT]: " GITACCOUNT
if [[ -z $GITACCOUNT ]]; then
GITACCOUNT=$DEFAULT_GITACCOUNT
fi
echo "### Personal token with REPO:DELETE rights [$DEFAULT_GITRIGHT_DELETE]: "
read -s GITRIGHT_DELETE
if [[ -z $GITACCOUNT ]]; then
GITRIGHT_DELETE=$DEFAULT_GITRIGHT_DELETE
fi
echo "### Personal token with REPO:* rights [$DEFAULT_GITRIGHT_CREATE]:"
read -s GITRIGHT_CREATE
if [[ -z $GITACCOUNT ]]; then
GITRIGHT_CREATE=$DEFAULT_GITRIGHT_CREATE
fi
# check if .git directory exists (cloned already)
if [ ! -d ".git" ]; then
# git remote add origin https://github.com/$GITACCOUNT/$GITREPO.git
# git remote -v
read -p "No local clone found. Shall I clone 'https://github.com/$GITACCOUNT/$GITREPO'? [y]" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]] || [[ -z $REPLY ]]
then
# clone remote repository to local
echo "[I] Clone remote repo to local"
git clone https://github.com/$GITACCOUNT/$GITREPO .
fi
fi
# check if .git directory exists now
if [ -d ".git" ]; then
read -p "Shall I remove repo 'https://github.com/$GITACCOUNT/$GITREPO'? [y]" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]] || [[ -z $REPLY ]]
then
# remove remote repository
echo "[I] Remove remote repo"
curl -X DELETE -H "Authorization: token $GITRIGHT_DELETE" "https://api.github.com/repos/$GITACCOUNT/$GITREPO"
fi
read -p "Shall I squash local repo and upload it? [y]" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]] || [[ -z $REPLY ]]
then
# Remove the history from local repository
echo "[I] Remove local repo settings"
rm -rf .git
# recreate the repos from the current content only
echo "[I] Initialize local repo"
git init
git add .
git commit -m "Initial commit"
echo "[I] Initialize remote repo"
# push to the github remote repos ensuring you overwrite history
git remote add origin https://$GITACCOUNT:$GITRIGHT_CREATE@github.com/$GITACCOUNT/$GITREPO.git
echo "[I] Squash local repo"
git reflog expire --expire=now --all && git gc --prune=now
git reflog expire --expire=1.minute refs/heads/master
git fsck --unreachable # now I see those tarball blobs!
git prune # hasta la vista, baby
git gc # cleanup and repack the repo
echo "[I] Push local repo to remote"
git push -u --force origin master
fi
read -p "Would you check remaining events of 'https://github.com/$GITACCOUNT/$GITREPO'? [y]" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]] || [[ -z $REPLY ]]
then
echo "[I] Extract remote events for repo"
# check events
curl https://api.github.com/repos/$GITACCOUNT/$GITREPO/events >"../$GITREPO.txt"
echo "---> check senstive data in file ../$GITREPO.txt"
fi
else
echo "No .git directory found - please enter valid repo directory!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment