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

Install SmartGit Git Client on Ubuntu, Linux Mint, CentOS, Fedora, RHEL

SmartGit is an efficient Git Client user interface with support for GitHub, Pull Requests + Comments, SVN as well as Mercurial....
28/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

Git – Push Changes to Remote Git Branch

In this article, I am going to show you how to push (upload) your local Git repository to a remote Git repository hosted...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024