pathScripts/gitpurge

34 lines
1 KiB
Text
Raw Permalink Normal View History

#!/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
# Check if git-filter-repo is installed
if ! command -v git-filter-repo &> /dev/null; then
echo "Error: git-filter-repo is not installed. Please install it first."
echo "You can install it via pip: pip install git-filter-repo"
exit 1
fi
# Get a list of files that currently exist in the repository
current_files=$(git ls-files)
# Create a file with the list of files to keep
echo "$current_files" > files_to_keep.txt
# Use git-filter-repo to keep only the files that currently exist
git filter-repo --paths-from-file files_to_keep.txt --force
# Remove the temporary file
rm files_to_keep.txt
# Force push all branches
git push origin --all --force
echo "Purge complete. All files not present in the local repo have been removed from all commits on all branches."
echo "The changes have been force-pushed to the remote repository."