Delete a Git Branch Locally
Git won’t allow you to delete a Git branch you are currently working on. So you must make sure to checkout to a branch that you are NOT deleting. For this use the command:
Syntax
git checkout <branch-name>
Here we will check out our main branch from my test branch.
Now in order to delete the test branch locally, we use the command :
Syntax
git branch -d <branch-name>
We will delete my test branch as an example.
Note: The -d option will delete the branch only if it has already been pushed and merged with the remote branch. If you want to forcefully delete a branch you will have to use the -D option instead. The -D flag is synonymous with –delete –force. This will forcefully delete the branch even if it hasn’t been pushed or merged with the remote. the full command is:
Syntax
git branch -D <branch-name>
With this, we can successfully delete a local branch.
How Delete a Git Branch Locally and Remotely
Git is a very powerful tool in the process of application development and is used widely in the software industry by developers to maintain the codebase. Using this developers are able to organize their codebase and manage the version history of their project. And, to do this developers create multiple branches so that team collaboration would become easier, and with time, it is important to delete those Git Branches, both Locally and remotely.
Now, as a developer, it is important for you to know how to delete a Git Branch Locally and Remotely as well because it will lead to a better productivity approach, and for that, we have discussed both ways to delete Git Branch. These are very simple git commands, which you can apply and get the desired result. But before that yous should know Why and when to Delete a Git Branches?
Contact Us