To delete a Git branch both locally and remotely, you can follow these steps:
Delete the local branch:
$ git branch -d branch_name
command:
git branch -d dev
output:
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Git doesn't allow you to delete a local branch if you are already checked out and in the same branch. To mitigate this problem, you just need to switch to a different branch and execute the command. The exact error that git throws is shown below.
command:
git branch -d dev
output:
error: Cannot delete branch 'dev' checked out at '/home/backendpro/TheBackendPro'
Push the change to the remote repository:
$ git push origin --delete branch_name
command:
git push origin --delete dev
output:
To https://github.com/ulasalasreenath/TheBackendPro.git
- [deleted] dev
This will remove the branch from your local and remote repository.