Auto-update: Tue Jul 30 20:47:09 PDT 2024

This commit is contained in:
sanj 2024-07-30 20:47:09 -07:00
parent fbbb92fe7a
commit e6195f4b8d

51
gitpurge Executable file
View file

@ -0,0 +1,51 @@
#!/bin/bash
# Ensure we're in a git repository
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
echo "Error: This script must be run inside a Git repository."
exit 1
fi
# Get the current branch name
current_branch=$(git rev-parse --abbrev-ref HEAD)
# Switch to the main branch
git checkout main
# Get a list of all files that have ever existed in the repository
all_files=$(git log --pretty=format: --name-only --diff-filter=A | sort -u)
# Get a list of files that currently exist in the repository
current_files=$(git ls-files)
# Find files that no longer exist
files_to_remove=$(comm -23 <(echo "$all_files") <(echo "$current_files"))
# If there are no files to remove, exit
if [ -z "$files_to_remove" ]; then
echo "No files to remove."
git checkout "$current_branch"
exit 0
fi
# Create a new branch for our changes
new_branch="cleanup-$(date +%Y%m%d%H%M%S)"
git checkout -b "$new_branch"
# Remove the files from all commits
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch $(cat <<EOF
'"$files_to_remove"'
EOF
)' \
--prune-empty --tag-name-filter cat -- --all
# Force push the changes
git push origin "$new_branch" --force
# Switch back to the original branch
git checkout "$current_branch"
echo "Cleanup complete. New branch '$new_branch' has been created and pushed."
echo "Please review the changes in '$new_branch' before merging into main."