diff --git a/gitpurge b/gitpurge new file mode 100755 index 0000000..8590631 --- /dev/null +++ b/gitpurge @@ -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 <