Last active
June 27, 2021 14:40
-
-
Save nielsonsantana/e717259f0262f8fd7d85d566b1c49323 to your computer and use it in GitHub Desktop.
Bash script to delete local git branches that aren't on git remote server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Nielson Santana @nielsonsantana | |
# This code is under Criative Communs license [https://creativecommons.org/licenses/by/4.0/] | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
function gitprunelocalbranchs(){ | |
RED="\e[31m" | |
ENDCOLOR="\e[0m" | |
git fetch origin -p | |
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
REMOTE_BRANCHS=$(git branch --list --remotes) | |
BRANCHS_TO_DELETE=$( | |
git branch --list --format='%(refname:short)' | while read -r line ; do | |
result=$(echo $REMOTE_BRANCHS | grep "$line") | |
if [ "$result" == "" ] && [ "$CURRENT_BRANCH" != "$line" ]; then | |
echo "${line}" | |
fi | |
done | |
) | |
echo "This script delete local branchs that aren't on remote repository." | |
if [ "$BRANCHS_TO_DELETE" == "" ]; then | |
echo "No branchs to delete." | |
else | |
echo "Branchs to delete:" | |
echo -e "${RED}$BRANCHS_TO_DELETE${ENDCOLOR}" | |
read -p "Do you want delete these branchs? (y/N)" INPUT_VARIABLE | |
if [ "$INPUT_VARIABLE" = "y" ]; then | |
echo "$BRANCHS_TO_DELETE" | while read -r line ; do | |
if [ "$line" != "" ]; then | |
git branch -D "$line" | |
fi | |
done | |
fi | |
fi | |
} | |
gitprunelocalbranchs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install guide