How to Delete a Git Branch

28/12/2020
Git
Creating branches in Git is easy. It’s considered one of Git’s best features. However, due to the presence of remote and local repositories, deleting branches can become a little confusing.

In this lesson, we will do the following:

  • Create a remote repository
  • Create a local copy of the remote repository
  • Create two branches in the local copy
  • Push one branch to the remote repository
  • Delete local branches
  • Delete remote branches

The lesson should give you an overall understanding of the branch creation and deletion process, so you have a good command over the necessary steps when you need to delete a local or remote branch.

Let’s get started.

1. Creating a Remote Repository

Let’s create a folder called project.git and initialize to be the remote repository:

$ mkdir project.git
$ cd project.git
$ git init –bare

Initialized empty Git repository in /Users/zak/_work/LearnGIT/git_delete_branch/project.git/

2. Creating a Local Copy of the Remote Repository

In a new location, let’s create a local copy called project_local of the remote repository using the clone command.

Note: If you are working with the GitHub or BitBucket, you’ll follow the same process to clone the repository. In that case, you will have an SSH link instead of the full file path used here.

$ git clone /Users/zak/_work/LearnGIT/git_delete_branch/project.git project_local

Cloning into ‘project_local’
warning: You appear to have cloned an empty repository.
done.

3. Creating Branches Inside the Local Copy

Let’s first add a file to the local copy and then push it to the remote repository:

$ cd project_local
 
$ touch ReadMe.txt
 
$ git add -A
 
$ git commit -m "Initializing the Module"
 
[master (root-commit) 81eb2a3] Initializing the Module
1 file changed, 0 insertions(+), 0 deletions()
create mode 100644 ReadMe.txt
 
$ git push origin master
 
Counting objects: 3, done.
Writing objects: 100% (3/3), 221 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /Users/zak/_work/LearnGIT/git_delete_branch/project.git
* [new branch]      master –> master

In the above commands, we created a file called ReadMe.txt, added it to the local copy, committed it to the local copy, and then pushed the changes to the remote repository or origin’s master branch.

If you check the branches, you’ll see the master branch in the local copy:

$ git branch
* master

If you check the remote branches, you will see the master branch there also:

$ git branch -r
origin/master

Hint: You can use the ‘-a’ option to see all branches in local and remote repositories together.

$ git branch -a
* master
remotes/origin/master

Let’s create two branches called b1 and b2 from the master branch:

$ git branch b1
$ git branch b2

Let’s check if the branches were created:

$ git branch
 
b1
b2
* master

Now we are going to make some modifications to the branches:

$ git checkout b1
 
Switched to branch ‘b1’
 
$ touch branch1.txt
 
$ git add -A
 
$ git commit -m "Branch1 modification"
 
[b1 a2f488e] Branch1 modification
1 file changed, 0 insertions(+), 0 deletions()
create mode 100644 branch1.txt
 
$ git checkout b2
 
Switched to branch ‘b2’
 
$ touch branch2.txt
 
$ git add -A
 
$ git commit -m "Branch2 modification"
 
[b2 2abb723] Branch2 modification
1 file changed, 0 insertions(+), 0 deletions()
create mode 100644 branch2.txt

Let’s check local and remote branch statuses:

$ git branch
 
b1
* b2
master
 
$ git branch -r
 
origin/master

We can see locally we have three branches master, b1, and b2. But we have only the master branch in the remote repository.

4. Pushing Branches to Remote Repository

Let’s push the b1 branch to the remote repository:

$ git push origin b1
 
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 249 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To /Users/zakh/_work/LearnGIT/git_delete_branch/project.git
* [new branch]      b1 –> b1

You can check the local and remote branch statuses:

$ git branch
 
b1
* b2
master
 
$ git branch -r
 
origin/b1
origin/master

From the above branch statuses, we can see that the b1 branch is available remotely also.

5. Deleting Branches Locally

You can delete branches locally with the -d or -D option.

git branch -d <branch_name>

Let’s first check into the master branch, so we can delete the b1 and b2 branches.

$ git checkout master
 
Switched to branch ‘master’
Your branch is up-to-date with ‘origin/master’.

Let’s try the -d option first to delete the branch b1:

$ git branch -d b1
 
error: The branch ‘b1’ is not fully merged.
If you are sure you want to delete it, run ‘git branch -D b1’.

The error is telling you that you have to merge the changes from branch b1. This is a safeguard, so you don’t mistakenly lose your work on branches. You can use the -D option to force delete the merge. But in this case, let’s merge the changes from b1 and b2 into master and push it to the remote repository.

$ git merge b1
 
Updating 81eb2a3..a2f488e
Fast-forward
branch1.txt | 0
1 file changed, 0 insertions(+), 0 deletions()
create mode 100644 branch1.txt
 
$ git merge b2
 
Merge made by the ‘recursive’ strategy.
branch2.txt | 0
1 file changed, 0 insertions(+), 0 deletions()
create mode 100644 branch2.txt
 
$ git push origin master
 
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 454 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To /Users/zak/_work/LearnGIT/git_delete_branch/project.git
81eb2a3..34db496  master –> master

Now try to delete the branches again:

$ git branch
 
b1
b2
* master
 
$ git branch -d b1
 
Deleted branch b1 (was a2f488e).
 
$ git branch -d b2
 
Deleted branch b2 (was 2abb723).
 
$ git branch
 
* master

You have successfully deleted the b1 and b2 branches locally.

6. Deleting Remote Branches

When you check the remote branches, you still see b1 present:

$ git branch -r
 
origin/b1
origin/master

You can use the following command to delete a remote branch:

git push <remote_name> –delete <branch_name>

So you can delete the remote b1 branch with the following:

$ git push origin –delete b1
 
To /Users/zakh_eecs/_work/LearnGIT/git_delete_branch/project.git
[deleted]         b1

Now if you check your remote branches, you shouldn’t see b1 anymore:

$ git branch -r
 
origin/master

Congratulations! You have successfully deleted all the branches you created. Practice making more branches and deleting them to master the Git branch deletion process.

Further Study:

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to Git Rebase

git rebase is a merge utility just like git merge. But the way they work is different. In this article, I will talk about...
29/12/2020

Installing and Getting Started with Git on Debian 10

Git is a very popular Version Control System (VCS). It is used to track changes in source codes during software development....
29/12/2020

How to Use git blame

git blame is a very good tracking command for Git. git blame shows the author information of each line of the project’s...
29/12/2020