Deleting a git branch locally and remotely is a common problem. This tutorial will show you how to do exactly that!
For our example, we'll be creating a new branch called
feature/new-feature and then deleting it both locally and remotely.
Step 1: Creating a new branch
Assuming you have a git repository, you can create a new branch by running the following command in your terminal:
$ git branch feature/new-feature
Step 2: Pushing the new branch to the remote origin
You can then push this new branch to your remote origin by running the following command:
$ git push origin feature/new-feature
Step 3: Deleting the branch locally
To delete the branch locally, you can run the following command:
$ git branch -d feature/new-feature
This will delete the branch locally and will not delete the branch on the remote origin. You'll see the following output:
Deleted branch feature/new-feature (was 44d5e57).
Step 4: Deleting the branch remotely
To delete the branch remotely, you can run the following command:
$ git push origin :feature/new-feature
Note that the colon is required. By prefixing the branch name with a colon, you are telling git to delete the branch on the remote origin.
You should see the following output:
- [deleted] feature/new-feature
You now know how to create a new branch, push it to the remote origin, and delete it both locally and remotely!