To delete a branch both locally and remotely, you need to use specific Git commands to remove the branch from the online repository and/or 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 below command.
git branch -d [BRANCH-NAME]
Anyway, the following image shows you a remote repository that contains 3 branches. I cloned already all branches except one which under the name “development”.
So I will delete “new-file” branch from the local repository. But before that I will show you a figure to understand all branches with its commit refs.
Let’s see how to delete the “new-file” branch from the local repository.
Delete a Branch Locally from the Repository
Actually, git system will not allow you delete the current repository. So you have to make sure that you already on another cursor (branch). Otherwise it will show you the following error.
So you have to move the cursor to another branch using the select command:
git switch [branch-name]. Then you can delete it directly by the below command.
git branch -d new-file
Or you can delete it permanently by using the -D flag instead of -d. So it would be written like the below command.
git branch -D new-file
But, if you look at the above figure you will see 2 refs committed there, which are e7f1da9 and 1e2439a, and the question is: does the deletion have any effect on that? Let’s see the answer in the following part.
Effecting of the Branch Deletion on its Logs
Actually, that doesn’t affect the commits and structure; it only removes the label name of the branch. But if those commits have not already been merged into the branch, then deleting the branch will delete all of its commits and refs to free up space.
I deleted the ‘new-file’ branch, but the two refs are still there to allow me to recreate the branch with the same name.
Check the following image.
Note: the branch deletion only effects on the local repository, but what about the remote repository? Let’s move to the next part to see how to upload this change.
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 below command.
git push origin :[BRANCH-NAME]
Or you can use another command
git push origin --delete [BRANCH-NAME]
In conclusion, deleting a branch in Git can be done locally or remotely. When deleting a branch locally, it only 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 the track, Git will delete all the commits and refs to free the space. Deleting a branch locally does not automatically delete it from the remote repository.
To delete a branch from the remote repository, one has to use a specific command. Finally, it is essential to ensure that you are on a different branch before attempting to delete the current branch locally.
To learn more visit the official page.