forked from denysdovhan/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-cleanup
executable file
·44 lines (35 loc) · 1.1 KB
/
git-cleanup
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
#!/usr/bin/env bash
# Removes old Git branches and does other cleanup
#
# Usage:
# git-cleanup [--force]
#
# See explanation here: http://erikaybar.name/git-deleting-old-local-branches/
#
# Author: Artem Sapegin, sapegin.me
# License: MIT
# https://github.com/sapegin/dotfiles
# Exit on any failed command
set -e
CYAN="$(tput setaf 6)"
UNDERLINE="$(tput sgr 0 1)"
NOCOLOR="$(tput sgr0)"
function header() { echo -e "\n$UNDERLINE$CYAN$1$NOCOLOR\n"; }
function branches() {
git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}'
}
if [[ "$1" != "--force" ]]; then
branches
exit 1
fi
# Delete all unreachable objects from the object database
header "Deleting unreachable objects..."
git prune
# Delete all stale remote-tracking branches, these branches have already been
# removed from the remote repository, but are still locally available in "remotes/".
header "Deleting stale remote-tracking branches..."
git remote prune origin
echo "Done."
# Delete branches for which remote branches were removed
header "Deleting branches with no longer existing remote branches..."
branches | xargs git branch -D