How to delete a branch

Let's see how we can delete a branch both locally and remotely.

case #1: delete a local branch

Let’s consider the following commit graph:

alina

e0d52ce457ace8b187885mainHEAD

Now, let’s create a branch b1 that points to the commit 457ace8:

alina

alina git:( main ) git branch b1 457ace8

alina git:( main )

This results in the following local commit graph:

alina

e0d52ce457ace8b187885b1mainHEAD

The branch b1 is a local branch, meaning it only exists on the client side since it hasn’t been pushed to the server. The server is unaware of its existence, and its commit graph remains unchanged:

arenas.git

b187885mainHEAD

In this scenario, we can delete the local branch b1 using git branch -d b1:

alina

alina git:( main ) git branch -d b1

Deleted branch b1 (was 457ace8).

alina git:( main )

With the deletion of b1, the local commit graph now appears as follows:

alina

e0d52ce457ace8b187885mainHEAD

case #2: delete both a local and a remote branch

Now, let’s recreate the branch b1:

alina

alina git:( main ) git branch b1 457ace8

alina git:( main )

But this time, let’s push the branch and its associated commits to the remote repository:

alina

alina git:( main ) git push

Enumerating objects: 7, done.

Counting objects: 100% (7/7), done.

Delta compression using up to 8 threads

Compressing objects: 100% (5/5), done.

Writing objects: 100% (6/6), 552 bytes | 552.00 KiB/s, done.

Total 6 (delta 0), reused 0 (delta 0), pack-reused 0

remote: . Processing 1 references

remote: Processed 1 references in total

To ssh://remote.mygit.com/gitpowerup/arenas.git

   b187885..e0d52ce  main -> main

alina git:( main ) git switch b1

Switched to branch 'b1'

alina git:( b1 ) git push --set-upstream origin b1

Total 0 (delta 0), reused 0 (delta 0), pack-reused 0

remote: . Processing 1 references

remote: Processed 1 references in total

To ssh://remote.mygit.com/gitpowerup/arenas.git

 * [new branch]      b1 -> b1

branch 'b1' set up to track 'origin/b1'.

alina git:( b1 )

As a result, the local commit graph looks like this:

alina

e0d52ce457ace8b187885b1mainorigin/b1origin/mainHEAD

Meanwhile, the remote commit graph appears as follows:

arenas.git

e0d52ce457ace8b187885b1mainHEAD

Now, let’s attempt to delete the branch b1 as we did before:

alina

alina git:( b1 ) git branch -d b1

error: Cannot delete branch 'b1' checked out at '/path/to/repo'

alina git:( b1 )

However, an error occurs because we cannot delete the branch that is currently active. To resolve this, let’s switch back to the main branch and try again:

alina

alina git:( b1 ) git checkout main

Switched to branch 'main'

Your branch is up to date with 'origin/main'.

alina git:( main ) git branch -d b1

Deleted branch b1 (was 457ace8).

alina git:( main )

This time, the deletion works successfully. As a result, the local commit graph looks like this:

alina

e0d52ce457ace8b187885mainorigin/b1origin/mainHEAD

Despite the deletion, the remote repository still retains the branch b1. To delete the remote branch, use the command git push origin --delete b1:

alina

alina git:( main ) git push -d origin b1

remote: . Processing 1 references

remote: Processed 1 references in total

To ssh://remote.mygit.com/gitpowerup/arenas.git

 - [deleted]         b1

alina git:( main )

After executing this command, the remote commit graph now appears as follows:

arenas.git

e0d52ce457ace8b187885mainHEAD

Additionally, the local commit graph no longer includes the reference origin/b1.

alina

e0d52ce457ace8b187885mainorigin/mainHEAD