Using the pipe operator, we passed the output of git branch into the grep. Our grep command highlighted the matching branches and each matched phrase. Now, let’s try to find all of the branches that we want to delete: $ git branch | grep -iE 'FIX|FEAT' ![]() Running git branch returns all of the existing local branches. To visualize this, let’s run each of these commands individually: $ git branch As a result, all branches that start with “FEAT” or “FIX” (case insensitive) are automatically deleted.xargs, or extended arguments, will take the left side of the last pipe (our grep command) and pass it as arguments to git branch -D, calling the branch deletion command for each matched branch. Third, we define the normal branch deletion command on the right side of the last pipe, and also supply xargs as part of this command.We also may have case variations between “FIX”, and “fix”, meaning our grep should be case-insensitive. This allows us to match several strings while also supporting extended regular expressions. To match several branch patterns at once, we define the -E flag. Since we are using a pipe, the output of git branch is passed to grep. Second, we define a grep command which will search the output of our branch command.This lists all of the branches within your project. First, we define our first command, git branch, on the left side of the first pipe.Let’s take a look at what’s being done here: The commandĭeleting branches in bulk can be done in a single line: git branch | grep -iE 'FIX|FEAT' | xargs git branch -D Thankfully, there’s an easier alternative to this, and we can run a single command using commands like a pipe ( |) and grep to delete several branches at once. If you forget to delete your local branches after merging your work, you may end up having weeks of stale branches living within your project. At any time, I have between 5-15 branches per project, all of which I may complete at random times based on priority and state. Note that if you delete a remote branch "X" from the command line using git push then it will also remove the local remote-tracking branch "origin/X" so there is no need to prune the obsolete remote-tracking branch with git fetch –prune or git fetch –p.Deleting several Git branches at once with commands like git branch -D can become a real headache. To delete a particular local remote-tracking branch, you can use following command: A shorter version of the command is below: This will delete all the obsolete remote-tracking branches. git remote prune origin "deletes the refs to the branches that don't exist on the remote. Now, you need to delete the local references too. If you have deleted a remote branch using the command git push origin :, its references still exist in local code repo of your team members. It means it exists on our local machine cache but not on the remote repository. These are local branches that have a direct association with a remote branch. When we check out a local branch from a remote branch, it automatically creates what is called a tracking branch. What are tracking branches and how to delete them It will also remove the branch forcibly even if there are unmerged changes in the branch. ![]() Here is the command to delete branch locally: Please note that deleting a branch locally will not delete the remote branch. Your code repository should be neat, tidy, and easy to navigate. You should perform periodic cleanup of the branches where you would either remove the old branches or you would merge them into the master. You need to ensure that your Git repository is not a mess of outdated and old branches that are not being worked on anymore.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |