Delete a Remote Branch

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Git, a branch is a lightweight movable pointer to a commit. Branches are used to develop features, isolate changes, and experiment without affecting other parts of the repository. Remote branches are references to the state of branches on remote repositories. They are used to keep track of the progress of other developers' work and to collaborate on projects.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/CollaborationandSharingGroup(["`Collaboration and Sharing`"]) git/CollaborationandSharingGroup -.-> git/remote("`Manage Remotes`") subgraph Lab Skills git/remote -.-> lab-12723{{"`Delete a Remote Branch`"}} end

Delete a Remote Branch

Sometimes, you may need to delete a remote branch that is no longer needed. For example, if a feature branch has been merged into the main branch, you may want to delete the remote feature branch to keep the repository clean.

Suppose that a GitHub repository called git-playground has been cloned from your GitHub account, which comes from a fork of https://github.com/labex-labs/git-playground.git. You want to delete the remote branch named feature-branch that is no longer needed. Here are the steps to delete the remote branch:

  1. Clone the repository, navigate to the directory and configure the identity:
    git clone https://github.com/your-username/git-playground.git
    cd git-playground
    git config --global user.name "your-username"
    git config --global user.email "your-email"
  2. Add the feature-branch branch to the origin remote repository:
    git checkout -b feature-branch
    git push -u origin feature-branch
  3. Use the git branch -r command to list all the remote branches.
    git branch -r
    The output should include the feature-branch remote branch:
    origin/HEAD -> origin/master
    origin/feature-branch
    origin/master
  4. Use the git push -d <remote> <branch> command to delete the specified remote <branch> on the given <remote>.
    git push -d origin feature-branch
    This command deletes the feature-branch remote branch on the origin remote repository.
  5. Use the git branch -r command again to verify that the remote branch has been deleted.
    git branch -r
    The output should not include the feature-branch remote branch:
    origin/HEAD -> origin/master
    origin/master

Summary

Deleting a remote branch is a simple process that involves using the git push -d <remote> <branch> command. This command deletes the specified remote <branch> on the given <remote>. By deleting remote branches that are no longer needed, you can keep your repository clean and organized.

Other Git Tutorials you may like