1. Overview
In this tutorial, We'll learn how to delete the local branch and remote branch in git with normal and force delete options.
Git provides easy access and manages the git local branches. And also provides exceptional support to remove local branches without any exception.
Removing git local branches can be done in two ways and removing remote repo in one way with commands.
let us look one by one now.
It is very useful to know Git Commands.
2. Git Remove Local Brach With -d option (d for normal or soft delete)
Git provides the useful command to work with the local branch deletion.
Git Delete local branch command syntax
git branch -d <branch-name>
This command deletes the branch completely from the local machine but does not remove the branch from the remote location.
Sometimes, you may get the error "error: cannot delete branch" as below.
Kotlin-Demo venkateshn$ git branch -d demo error: Cannot delete branch 'demo' checked out at '/Users/Documents/workspace/git-demo/Kotlin-Demo'
To avoid "cannot delete branch" error, you need to switch to the master branch first and then run the git branch -d command with the delete branch name.
After deleting the branch, you can not see the deleted branch on local but not deleted on the remote.
mac-MacBook-Pro-2:Kotlin-Demo $ git branch * demo master mac-MacBook-Pro-2:Kotlin-Demo $ git branch -d demo error: Cannot delete branch 'demo' checked out at '/Users//workspace/git-demo/Kotlin-Demo' mac-MacBook-Pro-2:Kotlin-Demo $ git branch -D demo error: Cannot delete branch 'demo' checked out at '/Users//workspace/git-demo/Kotlin-Demo' mac-MacBook-Pro-2:Kotlin-Demo $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. mac-MacBook-Pro-2:Kotlin-Demo $ git branch -d demo Deleted branch demo (was f984e75). mac-MacBook-Pro-2:Kotlin-Demo $ git branch * master mac-MacBook-Pro-2:Kotlin-Demo $ git branch -r origin/HEAD -> origin/master origin/demo origin/master mac-MacBook-Pro-2:Kotlin-Demo $
3. Git Force Delete Local Branch with Option -D
Sometimes, if you have some committed changes on your local branch but those are not on remote or vice versa.
In such cases, git will not allow deleting these branches with the "git branch -d" option. Because the local branch is not in sync with the remote branch. This is a legal and legitimate use case.
If you are sure about these changes are not needed for the future and you want to remove the branch on local then you need to mention to the git delete forcibly.
This is informed the git with hyphen D option (-D) on git branch command.
Syntax
git chekout master git branch -D <branch-name>
Example
MacBook-Pro-2:Kotlin-Demo $ git branch * demo master MacBook-Pro-2:Kotlin-Demo $ touch dummy.txt MacBook-Pro-2:Kotlin-Demo $ echo "this is a dummy file">>dummy.txt MacBook-Pro-2:Kotlin-Demo $ cat dummy.txt this is a dummy file MacBook-Pro-2:Kotlin-Demo $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. MacBook-Pro-2:Kotlin-Demo $ git branch -d demo error: The branch 'demo' is not fully merged. If you are sure you want to delete it, run 'git branch -D demo'. MacBook-Pro-2:Kotlin-Demo $ git branch -D demo Deleted branch demo (was a0d3b53). MacBook-Pro-2:Kotlin-Demo $ git branch * master MacBook-Pro-2:Kotlin-Demo $
4. How to undo Deleted Local Branch
If you want to get the deleted local branch again back to your machine, then the original copy is available in the remote git repository.
You have to follow the below steps. But remote copy may not be the same if you unpushed commits on your deleted local branch.
git pull git checkout <branch-name>
Example
MacBook-Pro-2:Kotlin-Demo $ git pull Already up to date. MacBook-Pro-2:Kotlin-Demo $ git checkout demo Branch 'demo' set up to track remote branch 'demo' from 'origin'. Switched to a new branch 'demo' MacBook-Pro-2:Kotlin-Demo $ git branch * demo master MacBook-Pro-2:Kotlin-Demo $
5. Git Delete Remote Branch
Sometimes you need to delete the branch from the remote repository. In that case, the git branch -d option does not do the job.
So, we need to use the git push command with the --delete option.
Syntax
git push origin --delete <remote-branch-name>
Example
We can see the remote branch on GitHub before removal.
MacBook-Pro-2:Kotlin-Demo $ git branch * demo master MacBook-Pro-2:Kotlin-Demo $ git push origin --delete demo To https://github.com/JavaProgramTo/Kotlin-Demo.git - [deleted] demo MacBook-Pro-2:Kotlin-Demo $
Now the branch demo is deleted from the remote repo and verified on the remote.
Look at the below image after git removed the remote branch.
6. Conclusion
In this article, We've seen how to remove branches locally and remote from the git repository.
No comments:
Post a Comment
Please do not add any spam links in the comments section.