Git Local Branch Cleanup
After you create a number of branches with git, it becomes outdated and a maintenance issue since local branches are not synchronized with branches on the server or origin. Thus you have to manually clean up the branches that are on your local repo.
Personally, I do this between sprints. Exception to this would be when I have a task that gets worked on in mulitple sprints. Reason I do this after each sprint is because when the task has been completed, it is merged into the development branch of code. No sense in keeping the code in two different places (or branches).
Commands
This is the command that I run via the command line to clean up all of the local branches. An explaination of the command is below.
for b in $(git branch | grep -v -e dev -e master)
do
git branch -D ${b}
done
Command Breakdown
for b in $(git branch | grep -v -e dev -e master)
command is the start of the loop. It is in two parts. The first is the for loop and the second is filtering the output of the branches.
git branch
bashows all of the branches that have been created in my local repository.
grep -v -e dev -e master
filters out the branches that have "dev" or "master" in their name. The only issue that I have with this command is that if you prefix your feature branches with "dev", then those branches will not be cleaned up.
do
and done
are the beginning and ending of the for loop contents. These essentially would be the braces if you were writing a function for you C# and Java programmers.
git branch -D ${b}
is the command that will take each of the branches that is returned by the for loop and replace the variable with the branch name. Then then branch will be deleted.
Output
Each of the branches that are deleted, will be shown as output from the command.
Deleted branch addressformatting
Deleted branch displaycontact
Deleted branch displaycontact4283
Deleted branch displaycontactaddr
Deleted branch incidentmigration
Conclusion
Remember to clean your repository periodically. This would be especially important if you create a branch for each feature or bug that you work on like I do. During one sprint, I created about 5 different local branches because I checked out a teammates code to review it and to place my code within my own braches so that the changes can be peer reviewed and merged in.