-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremove_files_from_commit.sh
More file actions
executable file
·58 lines (46 loc) · 1.49 KB
/
remove_files_from_commit.sh
File metadata and controls
executable file
·58 lines (46 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
# Script to remove files listed in .gitignore from a specific commit
# Usage: ./remove_files_from_commit.sh <commit_id>
if [ $# -eq 0 ]; then
echo "Usage: $0 <commit_id>"
echo "Example: $0 abc123def"
exit 1
fi
COMMIT_ID="$1"
# Verify commit exists
if ! git rev-parse --verify "$COMMIT_ID" >/dev/null 2>&1; then
echo "Error: Commit $COMMIT_ID does not exist"
exit 1
fi
echo "Creating backup and removing .gitignore files from commit: $COMMIT_ID"
# Create a backup branch
BACKUP_BRANCH="backup-$(date +%Y%m%d-%H%M%S)"
git branch "$BACKUP_BRANCH"
echo "Created backup branch: $BACKUP_BRANCH"
# Set environment variable to suppress warning
export FILTER_BRANCH_SQUELCH_WARNING=1
# Get list of files to remove from current .gitignore
TEMP_FILE=$(mktemp)
grep -v "^#" .gitignore | grep -v "^$" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' > "$TEMP_FILE"
echo "Files patterns to remove:"
cat "$TEMP_FILE"
echo ""
# Use filter-branch to remove files
git filter-branch --force --index-filter "
while IFS= read -r pattern; do
if [[ -n \"\$pattern\" ]]; then
git rm --cached --ignore-unmatch -r \"\$pattern\" 2>/dev/null || true
fi
done < \"$TEMP_FILE\"
" --prune-empty HEAD
# Clean up
rm -f "$TEMP_FILE"
echo ""
echo "Filter completed!"
echo "Backup branch created: $BACKUP_BRANCH"
echo ""
echo "To restore if something went wrong:"
echo "git reset --hard $BACKUP_BRANCH"
echo ""
echo "To delete backup after confirming everything is OK:"
echo "git branch -D $BACKUP_BRANCH"