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.
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:
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.
And in this directory we shall be initializing a bare Git repository. This shall be done using
git init command with an argument
Now we shall create a new directory where we shall be cloning the empty Git repository that we just created.
Here in this new directory we shall be cloning the empty branch that we just created. This shall be done using command
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.
Now we shall use command
git remote to know the remote name. This remote name shall be required in the next step.
Once we have the remote name,
git push <remote name> <branch name> to push the changes.
Now we shall create another branch name, say releasebranch1, make some changes and push it to the remote.
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>
Using the above command shall delete the remote branch.
In this example, we learnt the usage of
git push origin command to delete remote branch.