Skip to content

Instantly share code, notes, and snippets.

@unrelentingfox
Created October 31, 2017 00:21
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 unrelentingfox/5a385fe5a0d1232364f43a5aa135010e to your computer and use it in GitHub Desktop.
Save unrelentingfox/5a385fe5a0d1232364f43a5aa135010e to your computer and use it in GitHub Desktop.
A bash script for removing .gitignore files from git history
#!/bin/bash
set -o errexit
if [ $# -eq 0 ]; then
echo "No directories specified. Exiting."
exit
fi
# make sure we're at the root of git repo
if [ ! -d .git ]; then
echo "Error: must run this script from the root of a git repository"
exit 1
fi
# get files from gitignore files in argument directories
files=""
for dir in "$@"
do
fileArr=()
if [ -d "$dir" ]; then
cd $dir
dir=$dir"/"
fileArr=( $(git ls-files -i --exclude-from=.gitignore) )
fileArr=( "${fileArr[@]/#/$dir}" )
files=$files$( IFS=$' '; echo "${fileArr[*]}" )
cd ..
else
echo "directory \"$dir\" does not exist. Exiting."
exit
fi
done
echo $files
echo "filter-branch"
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch $files" HEAD
# remove the temporary history git-filter-branch otherwise leaves behind for a long time
echo "rm refs"
rm -rf .git/refs/original/ && git reflog expire --all && git gc --aggressive --prune
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment