9/26/2023 0 Comments Git delete branch except masterNotice here that we don’t use the grep command because there’s nothing to filter from the response. 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 ![]() What I really like about using -d when deleting branches is that it skips branches that are not fully merged. Delete the master branch: This can be done by clicking the branch icon and then the delete option in front of the branch name. 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 Change the default branch from master to OhThisAmazingBranch: This can be done in Settings > Branches and setting the default branch. Maybe you've got another branch called staging that you wish to preserve as well. Just rename the branch name you are using in the grep command. git branch: list all local branches grep -v 'master': filter out the word 'master' from the list xargs git branch -D: delete all branches in the list Keeping more than 1 branch. 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. Delete all local branches except for master. 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. However, the task can be accomplished by setting some variables and piping git branch -r, grep, sed, and xargs git push. Git branch | grep -v "^* main$" | xargs git branch -d git doesn't have a command to delete all the remote branches with exceptions. The easiest way to target the main branch is to first check it out, then target the asterisk in the name: git checkout main Enter the command git branch -delete Alternatively, you can use a. ![]() This will skip any branch with main in its name, even if it’s something like new-domain. Step 4 Delete the Branch Locally Open the Command Window from the Git repository root.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |