
If you try to do this, Git will show you an error:Īs the error suggests, you'll need to use the -D flag instead: git branch -D local_branch_name

This is because the commits are not seen by any other branches and Git is protecting you from accidentaly losing any commit data. If the branch contains unmerged changes and unpushed commits, the -d flag will not allow the local branch to be deleted. The command for deleting a local branch that we just used doesn't work in all cases.

#in my case the other branch I have is master, so I'd do: #where branch_name is the name of the branch you want to move to So before deleting a local branch, make sure to switch to another branch that you do NOT want to delete, with the git checkout command: git checkout branch_name If you try to do so, you'll get an error that will look something like this: I want to delete the test2 branch, but it is not possible to delete a branch you are currently in and viewing. I am currently on the test2 branch as the (*) shows: To list out all the local branches, you use the following command: git branch Let's look into this in a bit more detail with an example. local_branch_name is the name of the branch you want to delete. It denotes that you want to delete something, as the name suggests.
#Remove branches code
So you've created a branch to hold the code for a change you wanted to make in your project. This helps you keep the codebase clean and organized. So to sum up – branches let you make changes to the codebase without affecting the core code until you're absolutely ready to implement those changes. And a new branch lets you do this without affecting the main code in any way. You might create a branch to edit it to make changes, to add a new feature, or to write a test when you're trying to fix a bug. When working on a big project, there is the main repository with all the code, often called main or master.īranching allows you to create new, independent versions of the original main working project. Git branches are a snapshot of a project and its changes, from a specific point in time.
#Remove branches how to
In this article, you will learn the basics about how to remove local and remote branches in Git. Git branch -D only deletes local branches, and not the remote versions in the repo.Git is a popular version control system and an essential tool in a web developer's toolkit.īranches are a powerful and integral part of working with Git. Always test the regular expression to select your branches git branch | grep foo before passing them to git branch -D. Use caution when executing this command, as it will be difficult to recover branches deleted by accident. We’ve avoided writing out each branch name one-by-one! Danger ⚠️ This has the same result as writing git branch -D fix/ticket-123 task/ticket-123 task/ticket-123-v2 which deletes each of these branches. Using backticks, we pass the branch names to the git branch -D command.

Now we have our list of filtered branches, we can delete them all.

Grep can use complex regular expressions to search, but here all we need is a simple string pattern, ticket-123. Grep then filters the branches by the pattern provided. This command lists all the branches and pipes the output to grep. Using grep, we can select only those branches, since the names have a common pattern: $ git branch | grep ticket-123 They should be deleted to keep our environment tidy. Ticket-123 was completed weeks ago and the branches associated with it are stale. Imagine if you type git branch at your terminal and you see: $ git branch git branch -D `git branch | grep your-regex` But if you have several branch names that match a regular expression, then you can delete them all at once. However, it can be tedious to delete many branches by typing each name individually. But if you’re like me, you don’t do this very regularly! When I type git branch into the terminal I’m confronted with dozens of dead branches.ĭelete local branches by typing git branch -D branch-name in your terminal. Branches quickly expire and are no longer needed, so it’s a good idea to delete them. After working for a while on a project, it’s likely that you’ll have a lot of branches checked out locally.
