From time to time we end up accumulating several branches in our repositories, and we end up remembering this only by running "git branch", isn't that right? Here's a tip on how to delete a branch both locally and remotely.
Removing a local branch
To remove a local branch in Git, simply run the following command:
git branch -d branch-name
If you receive the following error:
error: The branch 'branch-name' is not fully merged.
That means you have some recent commit in this branch, and deleting it would mean losing this commit.
If you want to save a commit, merge it with another branch. Otherwise, do a force delete with the parameter -D capital. Be careful not to lose anything important.
git branch -D branch-name
In the above commands, -d is an alias for --delete
and -D is an alias for --delete --force
.
Removing a remote branch
To delete a remote branch, you can use these two commands, even though you have not yet removed your local branch.
git push origin --delete branch-name
or
git push origin :branch-name
The last one is also capable of removing a tag.
In the above commands, origin is the shortcut to your remote repository. If you have another name for the remote repository, you need to change origin.
What's the size of a branch?
If this is your only reason to remove any branch, you probably have nothing to worry about.
There is no native command to know how much space a branch takes up, but if you think in terms of how git works, a branch is nothing more than a pointer to your commits.
If you have created a branch, filled it with heavy files and didn't merge it with any other branch, it will have the size of these files, so if it's useless, it's better to delete it.
If a branch has been merged with the master, deleting it will hardly make any difference in terms of space.