Introduction.

This tutorial will cover “How to delete the GIT branch both in Local and Remotely”. But why delete the branch in Git?
To develop any new features or fix the bugs in the repository we have to create a branch. And it’s a best practice to delete the branch when the branch is not in use.
GIT is the Version control system used to manage the source code of an application. Where many developers can work on the same source code with the Sync.
The advantages of GIT are merging the source codes, reverting, and managing the different versions.
We can create the GIT repositories for the Application and keep the repo’s on GitLab, GitHub, Bit bucket, and many more. Want to install GIT on Ubuntu refer to the link.
How to Delete GIT Branch on Local System
So let’s check the GIT branch that are present in the local or remote system by “git branch -a” command.
$ git branch -a OUTPUT: * UAT main remotes/origin/HEAD -> origin/main remotes/origin/development-branch remotes/origin/main remotes/origin/test-branch
The UAT and main branch are the local branches. The rest are the remote branches, and currently, we are at the UAT branch.
$ git branch -d UAT OUTPUT: error: Cannot delete branch 'UAT' checked out at '/home/tastethelinux/tastethelinux-Group'
So while deleting the UAT branch we got the error that we can’t delete the branch, it is due to we are in the UAT branch. Now let’s switch to main branch and then delete the UAT branch.
$ git checkout main OUTPUT: Switched to branch 'main' Your branch is up to date with 'origin/main'.
$ git branch -d UAT OUTPUT: error: The branch 'UAT' is not fully merged. If you are sure you want to delete it, run 'git branch -D UAT'.
Sometimes it happens the branch is not merged or committed, but we have to delete the branch. Then use the -D option the branch will get deleted forcefully.
$ git branch -D UAT OUTPUT: Deleted branch UAT (was 7ca9df2).
Instead of the -D option we can use –delete –force to delete the branch forcefully.
How to Delete GIT Branch Remotely
Now let’s delete the remote branch on the remote repository.
$ git branch -a OUTPUT: * UAT main remotes/origin/HEAD -> origin/main remotes/origin/development-branch remotes/origin/main remotes/origin/test-branch
So from the above branches, we have to delete the “remotes/origin/test-branch” branch.
$ git push origin --delete test-branch OUTPUT: Username for 'https://gitlab.com': amashmishra9 Password for 'https://[email protected]': To https://gitlab.com/amashmishra9/tastethelinux-Group.git - [deleted] test-branch
So from the above branches, we have deleted the “remotes/origin/test-branch” branch. Once the branch gets deleted use the “git fetch -p” command.
$ git fetch -p
The -p option is to remove the remote tracking that no longer exists on the remote repository.
Conclusion
In this article, we have looked at “how to delete the branch on the local system”, and “how to delete the branch remotely”. While deleting the local branch the error we got on our local system. How to delete the branch forcefully and to remove the remote tracking that no longer exists. Any feedback or any issue let us know in the comment section.
One Reply to “How to Delete GIT Branch Local and Remotely.”