-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-garbage-collect
executable file
·83 lines (68 loc) · 1.79 KB
/
git-garbage-collect
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
set -euo pipefail
function show_usage() {
cat <<USAGE
Usage: git-garbage-collect [ -hi ]
Cleans and garbage collects your local git repo.
Options:
-h Show this help and exit.
-i Interactive mode. Ask before removing orphan files.
Discusion:
After regular use your local git repo gets full of dangling references, old tags, and orphan files,
all of which can take up extra space and slow down git operations.
This script cleans these up. Run it occasionally, especially after a large change has been made
to the repo, such as a Carthage SDK update.
USAGE
}
interactive_mode=false
while getopts ":hi" option
do
case "$option" in
h) show_usage
exit 0
;;
i) interactive_mode=true
;;
*) echo ">>> Error: Unknown option '$OPTARG'." 1>&2
exit 1
;;
esac
done
if ! top_level="$(git rev-parse --show-toplevel)"
then
echo ">>> Error: This script must be run from in your local git directory." 1>&2
exit 1
fi
cd "$top_level"
# Make sure the current git repo is clean:
if [ -n "$(git status --porcelain)" ]
then
echo ">>> Error: Please commit your changes before continuing." 1>&2
exit 1
fi
# Remove old tags:
git fetch --prune origin '+refs/tags/*:refs/tags/*'
# Remove old local branches:
git branch --merged=master | grep -v master | xargs git branch -d || true
# Remove old remote branches:
git fetch --prune
# Remove orphaned files and directories:
if $interactive_mode
then
clean_count=$(git clean -dxn | wc -l)
if (( $clean_count > 0 ))
then
git clean -dxn
if ./tools/scripts/askYN "Remove these files?"
then
git clean -dxf
fi
fi
else
git clean -dxf
fi
# Do this twice to get rid of any dangling refs after the first time:
git gc
git prune
git gc
git prune