Created
October 31, 2017 00:21
-
-
Save unrelentingfox/5a385fe5a0d1232364f43a5aa135010e to your computer and use it in GitHub Desktop.
A bash script for removing .gitignore files from git history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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