/Delete_local_branches

This script is designed to delete all local branches that do not exist in the remote repository.

Primary LanguageShell

Delete_local_branches

This script is designed to delete all local branches that do not exist in the remote repository.

Here is a breakdown of what each line of the script does:

#!/bin/bash:

This is a shebang line that tells the system that this is a bash script.

git fetch:

This command fetches the latest changes from the remote repository.

local_branches=$(git branch | cut -c 3-):

This command gets a list of local branches and saves them in the variable local_branches. The cut -c 3- command is used to remove the * character and space that appear before the name of the current branch.

while read -r branch; do:

This starts a loop that will iterate over each branch in the local_branches variable.

if ! git show-ref -q --heads "refs/remotes/origin/$branch"; then:

This command checks if the branch exists in the remote repository. The ! before git show-ref negates the command, so it will return true if the branch does not exist in the remote repository.

git branch -D "$branch":

This command deletes the local branch if it does not exist in the remote repository.

done <<< "$local_branches":

This ends the loop that was started in line 4.

In summary, this script fetches the latest changes from the remote repository, gets a list of local branches, loops through each local branch and checks if it exists in the remote repository, and deletes the local branch if it does not exist in the remote repository.