To delete a branch both locally and remotely, you must employ specific Git commands to remove the branch from the online repository as well as from the local repository.
Table of Contents
The exact steps may vary depending on the specific Git workflow and repository hosting service being used, but typically involve using commands like
git branch -d or
git push --delete to delete the branch.
To delete a branch from your repository using git, you have to use the command below.
git branch -d [BRANCH-NAME]
The following image depicts a remote repository containing three branches. I have already cloned all the branches except the one under the name “development.”
Thus, I will delete the “new-file” branch from the local repository. But before doing so, I will show you a figure to help you understand all the branches along with their commit references.
Let’s see how to delete the “new-file” branch from the local repository.
Delete a Branch Locally from the Repository
Actually, the git system will not allow you to delete the current branch. So, you have to ensure that you are on another branch (cursor). Otherwise, it will display the following error.
Thus, you must move the cursor to another branch using the select command:
git switch [branch-name]. Then, you can directly delete it using the command below.
git branch -d new-file
Or, you can delete it permanently by using the
-D flag instead of
-d. Thus, it would be written as the command below.
git branch -D new-file
However, if you examine the above figure, you will notice two committed refs there, which are e7f1da9 and 1e2439a. The question then arises: does the deletion have any effect on these? Let’s explore the answer in the following section.
Effects of Branch Deletion on Its Logs
Actually, that doesn’t affect the commits and structure; it only removes the label name of the branch. However, if those commits have not already been merged into another branch, then deleting the branch will remove all of its commits and references to free up space.
I deleted the ‘new-file’ branch, but the two refs are still there, allowing me to recreate the branch with the same name.
Here is an image showing you that.
Note: The branch deletion only affects the local repository. But what about the remote repository? Let’s move on to the next part to see how to apply this change remotely.
Delete a Branch from the Remote Repository
You will not be able to upload the current local change to the remote repository. You have to delete the branch directly from the remote repository using the command below.
git push origin :[BRANCH-NAME]
Or you can use another command
git push origin --delete [BRANCH-NAME]
Let’s summarize it.
In conclusion, deleting a branch in Git can be accomplished both locally and remotely. When a branch is deleted locally, it merely removes the label of the branch and does not affect the commits and structure of the repository.
However, if the commits on that branch were not merged into another branch, Git will delete all the commits and references to free up space. Deleting a branch locally does not automatically delete it from the remote repository.
To delete a branch from the remote repository, one must use a specific command. Finally, it is crucial to ensure that you are on a different branch before attempting to delete the current branch locally.
Thank you for reading. Happy Coding!