![]() ![]() If you run it with no arguments, you get a simple listing of your current branches: git branch iss53 master testing Notice the character that prefixes the master branch: it indicates the branch that you currently have checked out (i.e., the branch that HEAD points to). Notice here that we donât use the grep command because thereâs nothing to filter from the response. The git branch command does more than just create and delete branches. So you can technically just run through the entire list to delete everything except the current branch. Lastly, whether using the -d or -D option for deleting branches, you will not be able to delete the current branch. If you want to delete all returned branches, youâll want to use -D instead: git branch | grep -v "." | xargs git branch -D Delete All Git Branches Except Current Lets see how to delete them all in once with this command line : git branch grep -v master xargs git branch -D. What I really like about using -d when deleting branches is that it skips branches that are not fully merged. ![]() Keeping the example of main, hereâs what you can do: git branch -format='%(refname:short)' | grep -v "^main$" | xargs git branch -d Force Deleting Git Branches If you donât want to worry about checking out a branch, but want to be precise, you can choose to print the branches as a list with no whitespace. ![]() Here, the ^ means "begins with" and $ means "ends with." Therefore, weâre matching exactly * main, which is how the main branch will be printed when it is checked out. The shell command above deletes every branch in your local checkout except for master branch. Git branch | grep -v "^* main$" | xargs git branch -d The easiest way to target the main branch is to first check it out, then target the asterisk in the name: git checkout main eg: IF, you created a branch that was, // not pushed it will remain along with amster. The git delete branch operation is considered a very good repository hygiene. Git delete all branches except master Comment 3 xxxxxxxxxx // - Clear all branches except Master Branch - git branch -D (git branch grep -v 'master') // This will clear all your branches you have on local that you have // not pushed to your repository. This will skip any branch with main in its name, even if itâs something like new-domain. We can use the command git push origin : to remove a remote branch if our Git version is before 1.7.0. We can use the git branch -delete branch-name command to delete the branch.![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |