Git

Git Delete Remote Branch Example

Let’s say our work with remote branch is complete, or let’s say one of the team was working on a feature which has been completed now and all the changes has already been merged into remote’s master branch. Now that we have no requirement of remote branch, we might want to delete it. In this example we shall learn the same.

1. A brief on Git Push Origin

Before we start with our example, we need to understand git push origin command.

git push command as we know push commits made on local branch to a remote repository.
This git push command basically takes two arguments:

  • A remote name, for example, origin
  • A branch name, for example, master

This makes the structure of command as:

git push

We shall be using this command with an additional attribute to perform the desired operation of deleting remote branch which we shall be doing in this example.

2. Git Delete Remote Branch in Action

To see our example in action, we shall start with creating git repository.

First we shall create a directory for master branch.

Create Directory for initializing Git Branch
Create Directory for initializing Git Branch

And in this directory we shall be initializing a bare Git repository. This shall be done using git init command with an argument --bare.

Initialize bare Git repository
Initialize bare Git repository

Now we shall create a new directory where we shall be cloning the empty Git repository that we just created.

Create directory for branch
Create directory for branch

Here in this new directory we shall be cloning the empty branch that we just created. This shall be done using command git clone.

Clone empty Git repository
Clone empty Git repository

Here at first we can see that git branch command won’t show any branch created yet.
So, we shall be creating a new file, we will use command git add and then git commit to commit in order to create a branch. Now using command git branch will show master branch created.

Add file to master branch
Add file to master branch

Now we shall use command git remote to know the remote name. This remote name shall be required in the next step.

Git remote name
Git remote name

Once we have the remote name, git push <remote name> <branch name> to push the changes.

Git push changes
Git push changes

Now we shall create another branch name, say releasebranch1, make some changes and push it to the remote.

Push changes to new branch
Push changes to new branch

Now that we have our remote branch created, and lets assume all the work that was required from this remote branch is complete, we intend to delete it. For this we shall be using command git push <remote name> --delete <branch name>

Delete remote branch
Delete remote branch

Using the above command shall delete the remote branch.

3. Conclusion

In this example, we learnt the usage of git push origin command to delete remote branch.

Saurabh Arora

Saurabh graduated with an engineering degree in Information Technology from YMCA Institute of Engineering, India. He is SCJP, OCWCD certified and currently working as Technical Lead with one of the biggest service based firms and is involved in projects extensively using Java and JEE technologies. He has worked in E-Commerce, Banking and Telecom domain.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button