The process of deleting GitHub branches is pretty simple. However, it depends on which interface you are working with and whether you are dealing with a local or remote office.
Read on to learn how branch deletion works in GitHub and how to delete removed branches in different circumstances. We also explain how to delete multiple branches at once, how to restore branches, and how to delete an entire repository if you wish.
What is a GitHub branch and why should you delete it?
Like other Git repository hosting platforms (e.g. GitLab), GitHub uses branches to help developers manage the source code. A branch is a set of changes made to code by a developer. By treating each changeset as a separate branch, it’s easy to decide which changes to keep and which to discard. In addition, branches can be “merged” to incorporate multiple sets of changes.
However, most developers don’t want to keep every GitHub branch they create indefinitely. Instead, they routinely delete branches once they’ve merged them with other branches. Developers can also choose to delete a branch if they have decided to discard the changes it contains, for example because the changes are buggy or relate to application features that the developers didn’t add.
Local vs. remote branches
There are two types of branches you can work with on GitHub: local and remote.
Local branches are branches of code stored on a developer’s local computer. These branches aren’t actually hosted on GitHub, but you might still want to delete them as part of a GitHub-centric workflow. For example, you might be working with code on a local branch that you are pushing to GitHub and decide to delete the local branch after merging it with a remote branch.
Remote branches are code branches hosted on a remote machine – such as B. GitHub servers. Any branches that exist on GitHub itself are remote branches from a local machine perspective, although again you may need to work with local branches in the context of using GitHub. In many cases, branches that end up on GitHub originate as local branches that a developer creates on his or her own machine and later pushes to GitHub.
What happens when you delete a remote branch on GitHub?
When you delete a remote branch (or a local branch), the branch disappears from your GitHub workflow. You can no longer track or view the branch, and the branch is removed from your remote repository.
Usually that’s what you want. If you decide to delete a branch, it’s probably because you don’t want to use it anymore and don’t want to be a distraction when working in a repository.
That said, there are situations where you might decide after the fact that you really didn’t want to delete a branch. Luckily, it is possible to recover deleted branches, as we explain later in this article.
Steps to delete remote branches on GitHub
Deleting remote branches hosted on GitHub is easy as long as you manage them from your GitHub account. Here are the steps:
- First, navigate to the main page of the repository that contains the branch you want to delete.
- Then click the branches Button. You will see a list of branches in the repository.
- To delete a branch, click the trash can icon next to its name.
Branch deletion from the command line
If you prefer to work from the CLI, you can also delete GitHub branches this way.
You must first set up Git on your machine and integrate it with GitHub by following the steps in the GitHub documentation.
Then you can use those git push [remotename] :[remotebranchname] Command to delete a branch. For example:
git push origin --delete :somebranch
This syntax may seem confusing as it uses the git push command instead git branch. Unfortunately, because git branch does not support deleting remote branches, so you need to manage remote branch deleting with Git.
While this CLI approach to removing remote branches works, the best way to remove a branch hosted on GitHub is to use the web interface.
Delete local branches with Git
Deleting local branches is easier. You can do this on the CLI with a command like the following:
git branch -d branchname
This command deletes a local branch with the name branch name.
Since Git doesn’t provide a web interface, deleting local branches from the command line is usually the only way to remove a local branch. The exception is if you’re running some kind of local Git web interface that supports deleting branches, but that’s rare. Most developers manage local branches via the CLI and push their code to GitHub (or a similar platform) if they want web-based management capabilities.
Delete all local branches
What if you want to delete all local branches within a repository at once? Instead of having to manually delete each branch individually, you can use the git command in connection with grep to select multiple branches to delete via pattern matching. For example:
git branch -D `git branch --merged | grep -v \* | xargs`
That grep -v /* The above command selects all branches.
Of course, keep in mind that this approach exposes you to accidental branch deletion, as you can accidentally select branches you didn’t intend. And Git doesn’t warn you before deleting. So if you decide to delete all local branches with a single command, proceed with caution.
How to restore Git branches
In the event that you accidentally delete a Git branch—or you deleted it on purpose but later decide you want it back—all is not lost. You can restore branches both locally and in GitHub.
To restore a local branch, first run the command:
git reflog --no-abbrev
This generates a SHA1 value that identifies the first commit associated with the deleted branch. With this information you can do the following:
git checkout [shastring]
To return to this commit from there, run:
git checkout -b branchname
This will recreate a new branch identical to your deleted branch.
You can use the GitHub web interface to restore deleted branches as long as they were part of completed pull requests. Do this by clicking first pull requests Button for the repository that hosted the fork. Then click Closed to view a list of completed pull requests. They realize restore branch Button that allows you to restore branches linked to 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 this:
rm -r /path/to/.git
Keep in mind that there is no way to recover a repository if you take this approach (besides trying Recover deleted data in your file systemwhich is a process that yields spotty results at best).
Alternatively, you can delete repositories via GitHub by clicking on the repository’s settings press and then click Delete this repository. GitHub provides a process for restore deleted repositories in most cases, as long as it’s been 90 days or less since you deleted the repository.
Changing a branch name
It is possible to change a branch name on Git using the CLI. First you need to check out the branch you want to rename with a command like this:
git checkout branchname
Then rename it with the git branch -m Command:
git branch -m newbranchname
On GitHub you can rename a branch by clicking branches button in a repository and then click the Edit button (which looks like a pen).
Conclusion: delete with caution
Both the Git CLI and the GitHub web interface make working with branches easy. You can delete, rename, and (in most cases) even restore branches.
However, the process for completing these tasks varies a bit depending on whether you’re working with a local or remote branch office. And remember that while many changes to branches can be undone, this isn’t always the case. So you should be careful before removing a branch (or even more an entire repository).
About the authorChristopher Tozzi is a technology analyst with expertise in cloud computing, application development, open source software, virtualization, containers and more. He also teaches 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.