How To Delete Local And Remote Branches In Git: A Complete Guide
Deleting a Local Branch in Git
Local Branch vs. Remote Branch in Git
Understanding the difference between local and remote branches is crucial for working with Git effectively.
Local Branch
Remote Branch
Key Differences Between Local and Remote Branches
Understanding with an example
Let’s walk through a practical example.
Step 1: Cloning the Repository
Let us clone a basic repo from GitHub
git clone [email protected]:TvisharajiK/sample.git
#git shows the below
Cloning into 'sample'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (3/3), done.
Navigate to the cloned directory and check the available branches:
cd sample
git branch -a
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
Currently, we have only the main branch in the sample repository.
Step 2: Working with Local Branches
Let's create a local branch named try
$ git checkout -b try
Switched to a new branch 'try'
Deleting the Feature Branch
Now, try to delete the try branch using the -d option:
$ git branch -d try
error: Cannot delete branch 'try' checked out at '/Users/tvisharaji/sample'
You’ll encounter an error since you cannot delete the currently checked-out branch.
Switching to the Main Branch
Switch to the main branch to delete try:
$ git checkout main
Switched to branch 'main'
Now, delete the try branch again:
$ git branch -d try
Deleted branch feature (was 3aac499)
Deleting a Local Branch With the -D Option
Let’s recreate the try branch and make some changes:
$ git checkout -b try
$ echo "trying this" >> README.md
$ git add README.md
$ git commit -m 'Add to README'
Attempting to delete the branch with -d again, it will show an error because it contains unmerged changes:
$ git branch -d try
error: The branch 'try' is not fully merged.
Force Deletion
To force delete the branch, use:
$ git branch -D try
Deleted branch try (was 4a87db9)
Understanding Local Branch Deletion
It’s important to note that using -d or -D will only remove the local branch; any corresponding remote branch will remain unaffected.
Deleting a Remote Branch
To delete a remote branch, use one of the following commands:
For Git versions 1.7.0 and later:
git push origin --delete <branchName>
Creating and Pushing a Remote Branch
Let’s create a tryrem branch, make some changes, and push it to the remote repository:
$ git checkout -b tryrem
$ echo "trying for the Remote branch" > remote.txt
$ git add remote.txt
$ git commit -m 'Add remote.txt'
$ git push origin tryrem
Deleting the Remote Branch
To remove the remote tryrem branch:
$ git push origin --delete tryrem
After executing this command, the remote branch is deleted, but your local branch will still exist.
Conclusion
In this article, we explored how to delete local and remote branches in Git. Here’s a quick summary:
FAQ
What command do I use to delete a local branch in Git?
How do I delete a remote branch in Git?
What is the difference between a local branch and a remote branch?
Can I delete a branch that I’m currently on?
What happens if I try to delete a branch that has unmerged changes?
What are the consequences of deleting a local branch?
How can I verify if a branch has been deleted successfully?
What is the process for creating and pushing a new branch to a remote repository?