11/30/2023 0 Comments Git delete branch except master![]() ![]() ![]() For example, let’s say you wanted to list branches that do not include nodelete in the name. fi that we did in the function but if cat - ever fails then the xargs command will execute and you might end up deleting branches by accident. Force deleting branches Delete all branches except the current branch Delete All Git Branches Using an Exclusion Pattern You can list all git branches except a specific branch using the -v option in the grep command. Gcb () works instead of writing out the if. I created a dummy repo and a couple of branches: ![]() My gut tells me if you’re reading this, you might not need the extra details provided in the video, but just in case I’ve also done videos in the past about Unix pipes if you’re interested. Run a command for each item in the listīy the way the demo video goes into more detail and shows running these commands.Easily allow being able to do a dry run without really deleting the branches.Skip deleting the currently checked out branch even if it’s not in the whitelist because this could be a current work in progress branch that you will make a PR out of later.Whitelist 1 or more branches that will never get deleted such as main, master, production or whatever branch names you’ve chosen to be long running branches.List all of the branches in this git repo.The reason it’s not the end game solution is because we can turn it into a more general alias later (which we will).īefore we write any code, let’s whip up a little bit of pseudo code of how it’ll work: This won’t be our end game solution but it will work. To your surprise (or maybe not surprise) you might have dozens or even hundreds of old or unused branches.Ĭreating a 1 Liner to Clean Up Old Local Branches If you executed the above bullet points and locally ran git checkout main & git pull you would get the latest merged in changes but your local PR branch is still lingering around. In the end your remote repo doesn’t build up any branch cruft because chances are you configured your remote git service of choice to delete the PR branch after it’s been merged. Someone reviews that work and merges that PR into your main branch.You push that branch up and make a PR from it on GitHub, GitLab, Bitbucket, etc.You make a new local branch to do your work ( git checkout -b feature/something-cool).The use case around this is, let’s say you have a project you’re working on and: If you prefer video, I recorded a demo video on YouTube going over what’s written below with a number of examples. Quick Jump: Creating a 1 Liner to Clean Up Old Local Branches | Making It Easier to Use with a Function Alias | Demo Video Here's a 1 liner you can turn into an alias. Delete master branch in GitHub repository Go to your repository in GitHub using a web browser Click on Settings. Updated on November 15th, 2022 in #linux Delete Old or Unused Local Git Branches Using grep and xargs I like cleaning up unused branches and doing it manually can be tedious. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |