Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- #
- # git_cleanup.sh
- #
- # Cleans up local git repository, using origin/master as a
- # frame of reference.
- #
- # * Deletes references to remote branches that have since been removed.
- # * Deletes local branches that have been fully merged into master.
- # * Garbage collects local git repo.
- command -v git >/dev/null 2>&1
- if [ "$?" -ne "0" ]; then
- >&2 echo "git not found in PATH"
- exit 1
- fi
- git rev-parse --git-dir >/dev/null 2>&1
- if [ "$?" -ne "0" ]; then
- >&2 echo "not a git directory"
- exit 1
- fi
- ORIGIN="${ORIGIN:-origin}"
- MASTER="${MASTER:-master}"
- CURRENT=`git rev-parse --abbrev-ref HEAD 2>/dev/null`
- if [ -z "$CURRENT" ]; then
- >&2 echo "unable to get current branch"
- exit 1
- fi
- if [ "$CURRENT" != "$MASTER" ]; then
- >&2 echo "please checkout $MASTER branch before proceeding"
- exit 1
- fi
- git pull --ff-only >/dev/null 2>&1
- if [ "$?" -ne "0" ]; then
- >&2 echo "failed to fast-forward pull"
- exit 1
- fi
- # remove local refs to remote branches that no longer exist at the origin
- git fetch $ORIGIN -p
- if [ "$?" -ne "0" ]; then
- >&2 echo "failed to fetch $ORIGIN"
- exit 1
- fi
- # delete local branches that have been merged fully into master
- git branch --merged $ORIGIN/$MASTER | \
- egrep -v "(^\*|${MASTER})" | \
- xargs -r git branch -d
- # garbage collect
- git gc >/dev/null 2>&1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement