The process for deleting GitHub branches is pretty straightforward. However, it varies depending on which interface you're working with and whether you're dealing with a local or a remote branch.
Keep reading for a breakdown of how branch deletion works in GitHub and how to delete remote branches under various circumstances. We'll also explain how to delete multiple branches at once, how you can undelete branches, and how to delete an entire repository if you find yourself wanting to do so.
What Is a GitHub Branch, and Why Would You Delete It?
Like other Git repository hosting platforms (such as GitLab), GitHub uses branches to help developers manage source code. A branch is a set of changes that a developer has made to code. By treating each set of changes as a distinct branch, it's easy to decide which changes to keep and which to discard. In addition, branches can be "merged" in order to integrate multiple sets of changes.
That said, most developers don't want to retain every GitHub branch that they create for an indefinite period of time. Instead, they routinely delete branches once they've merged them into other branches. Developers might also choose to delete a branch if they've decided to abandon the changes contained in it because, for example, the changes are buggy or they address application features that the developers decided not to add.
Local vs. remote branches
There are two types of branches you can work with on GitHub: local and remote branches.
Local branches are branches of code stored on a developer's local machine. These branches aren't actually hosted on GitHub, but you might still want to delete them as part of a GitHub-centered workflow. For example, you may be working with code in a local branch that you push to GitHub, and decide to delete the local branch after it has been merged into a remote branch.
Remote branches are branches of code hosted on a remote machine — such as GitHub servers. All branches that exist on GitHub itself are remote branches from the perspective of a local machine, although again, you may have to work with local branches in the context of using GitHub. Indeed, in many cases branches that end up on GitHub originate as local branches that a developer builds on his or her own computer, and then later pushes to GitHub.
What Happens When You Delete a Remote Branch on GitHub?
When you delete a remote branch (or a local branch, for that matter), the branch disappears from your GitHub workflow. You'll no longer be able to track or view the branch, and the branch will be removed from your remote repository.
Usually, this is what you want to happen. If you decide to delete a branch, it's probably because you are done using it and you no longer want it to be a distraction when you are working within a repository.
That said, there are situations where you may end up deciding after the fact that you didn't actually want to delete a branch. Fortunately, it's possible to recover deleted branches, as we explain later in this article.
Steps for Deleting Remote Branches on GitHub
Deleting remote branches that are hosted on GitHub is easy, as long as you are managing them through your GitHub account. Here are the steps:
- Start by navigating to the main page of the repository that hosts the branch you want to delete.
- Then, click the Branches button. You'll see a list of branches in the repository.
- To delete a branch, click the trashcan icon next to its name.
Branch deletion through the command line
If you prefer to work from the CLI, you can delete GitHub branches that way, too.
You must first set up Git on your computer and integrate it with GitHub by following the steps in the GitHub documentation.
Then, you can use the git push [remotename] :[remotebranchname] command to delete a branch. For example:
git push origin --delete :somebranch
This syntax may seem confusing because it uses the git push command instead of git branch. Unfortunately, however, because git branch doesn't support deletion of remote branches, this is how you have to manage remote branch deletion with Git.
While this CLI approach works for removing remote branches, the best way to remove a branch hosted on GitHub is to use the web interface.
Deleting Local Branches with Git
Deleting local branches is simpler. You can do it on the CLI with a command such as:
git branch -d branchname
This command deletes a local branch with the name branchname.
Because Git doesn't provide a web interface, deleting local branches through the command line is typically the only way to remove a local branch. The exception is if you run some type of local Git web interface that supports branch deletion, but this is rare. Most developers manage local branches through the CLI and push their code to GitHub (or a similar platform) if they want web-based management features.
Deleting all local branches
What if you want to delete all local branches within a repository at once? Instead of having to delete each branch manually one-by-one, you can use the git command in conjunction with grep to select multiple branches for deletion via pattern matching. For example:
git branch -D `git branch --merged | grep -v \* | xargs`
The grep -v /* command above selects all branches.
Keep in mind, of course, that this approach leaves you liable to accidental branch deletion because you may inadvertently select branches that you didn't mean to. And Git won't warn you before deletion. So, if you decide to delete all local branches using a single command, proceed with caution.
How to Undelete Git Branches
In the event that you accidentally delete a Git branch — or you deleted it deliberately but decide later that you want it back — all is not lost. You can undelete branches both locally and in GitHub.
To undelete a local branch, first run the command:
git reflog --no-abbrev
This will generate a SHA1 value that identifies the first commit associated with the deleted branch. Using that information, you can run:
git checkout [shastring]
To get back to that commit, from there, run:
git checkout -b branchname
This will re-create a new branch that is identical to your deleted branch.
On the GitHub web interface, you can restore deleted branches as long as they were part of completed pull requests. Do this by first clicking the Pull requests button for the repository that hosted the branch. Then, click Closed to view a list of completed pull requests. You'll see a Restore branch button that allows you to recover branches associated with closed pull requests.
How to Delete a Git Repository
If you want to delete an entire local Git repository instead of just a branch, you can delete the .git directory on your computer with a command like:
rm -r /path/to/.git
Bear in mind that there is no way to undelete a repository if you take this approach (apart from trying to recover deleted data on your file system, which is a process that yields spotty results at best).
You can alternatively delete repositories through GitHub by clicking the repository's Settings button, then clicking Delete this repository. GitHub does provide a process for restoring deleted repositories in most cases as long as it has been 90 days or fewer since you deleted the repository.
Changing a Branch Name
It's possible to change a branch name on Git using the CLI. First, you have to check out the branch you want to rename with a command such as:
git checkout branchname
Then, rename it using the git branch -m command:
git branch -m newbranchname
On GitHub, you can rename a branch by clicking the Branches button in a repository, then clicking the edit button (which looks like a pen).
Conclusion: Delete with Caution
Both the Git CLI and the GitHub web interface make it easy to work with branches. You can delete, rename, and (in most cases) even undelete branches.
However, the process for accomplishing these tasks varies a bit depending on whether you are working with a local or a remote branch. And keep in mind that although many changes to branches can be reversed, that's not always the case, so you'll want to be careful before removing a branch (or, even more so, an entire repository).
About the authorChristopher Tozzi is a technology analyst with subject matter expertise in cloud computing, application development, open source software, virtualization, containers and more. He also lectures at a major university in the Albany, New York, area. His book, “For Fun and Profit: A History of the Free and Open Source Software Revolution,” was published by MIT Press.