![]() ![]() Unfortunately, in simple Git aliases, it is not possible to use the pipe operator correctly and forward the results of a command to another one. To set up these aliases you can execute: git config -global checkout Important aliases that you maybe already used: It packs commands into an alias so you can use it wherever you want. Git Aliases are a feature that improves your Git experience by making your workflow simple and easy. To avoid this issue you can adjust the command by adding a \|* in the end: git branch -merged | grep -v "main\|master\|develop\*" | xargs git branch -D Create A Git Alias Simple Git Aliases If you execute this script while you are on a branch that has changed it does not work correctly because there will be the following error: error: branch '*' not found. You can also use -d to abort if you forgot -merged. Now, you can pipe the result to the delete command introduced in the previous chapter: git branch -merged | grep -v "main\|master\|develop" | xargs git branch -DĮxecuting this command in any Git repository (where main/master is the protected branch) will delete every branch that is already merged and have no local changes. You can adjust the find command to your needs (for me it's main/master/develop): git branch | grep -v "main\|master\|develop"įurthermore, you can add the -merged flag to indicate that you only want to find branches that are already merged git branch -merged | grep -v "main\|master\|develop" If you execute this command in your CLI you will see that also your main and/or protected branches are included: master, main, release. With this in mind, we first write a command that finds all branches in your repository: git branch | grep -v \* To implement a function that does this it is important to know that git branch -D can handle several files at once. When You have multiple local branches, you probably want to delete them all with one command instead of executing the delete command for every single branch. However, if you want to delete a branch that is not merged you can use the capital D: git branch -D YOUR_BRANCH Remove All Local Branches Unfortunately (or luckily), this command will only work if the selected branch that you want to delete is already merged. Switch to your favorite terminal and use the following command to delete a branch: git branch -d YOUR_BRANCH However, you cannot call yourself a developer if you are not able to use the terminal correctly! To delete a single local branch you can open your favorite Git GUI interface and just press delete on the selected branch. ![]() Within this short tutorial, I will show how you can easily do this and how you can create a Git alias that you can simply reuse everywhere. ![]() After optimizing your Git workflow and starting working with Pull Requests you will probably have a ton of local branches that all have been merged already and could be deleted. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |