-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-from-git.sh
More file actions
66 lines (55 loc) · 1.78 KB
/
remove-from-git.sh
File metadata and controls
66 lines (55 loc) · 1.78 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
59
60
61
62
63
64
65
66
#!/bin/bash
# Check if a file path is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <file_path>"
exit 1
fi
FILE_PATH="$1"
# Function to ask for confirmation
confirm() {
read -r -p "${1:-Are you sure? [y/N]} " response
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
# Check if file exists
if [ ! -f "$FILE_PATH" ]; then
echo "File $FILE_PATH does not exist."
exit 1
fi
# Add to .gitignore
echo "Adding $FILE_PATH to .gitignore..."
echo "$FILE_PATH" >> .gitignore
git add .gitignore
git commit -m "Add $FILE_PATH to .gitignore"
# Remove file from git tracking
echo "Removing $FILE_PATH from git tracking..."
git rm --cached "$FILE_PATH"
git commit -m "Remove $FILE_PATH from repository"
# Push changes
if confirm "Do you want to push these changes to the remote repository? [y/N]"; then
git push
fi
# Remove file from git history
if confirm "WARNING: This will rewrite git history. Are you sure you want to proceed? [y/N]"; then
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch $FILE_PATH" \
--prune-empty --tag-name-filter cat -- --all
# Force push
if confirm "Do you want to force push these changes to the remote repository? This will overwrite history. [y/N]"; then
git push origin --force --all
git push origin --force --tags
fi
# Clean up
if confirm "Do you want to expire the reflog and run garbage collection? [y/N]"; then
git reflog expire --expire=now --all
git gc --prune=now --aggressive
fi
fi
echo "Process completed. Please review the changes and ensure everything is correct."
echo "Remember to recreate your $FILE_PATH locally if needed, and consider rotating any exposed secrets."