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-12637{{"`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.

Tasks

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.

  1. Open the terminal and navigate to the local repository directory.
  2. Add the feature-branch branch to the origin remote repository.
  3. List all the remote branches.
  4. Deletes the feature-branch remote branch on the origin remote repository.
  5. Verify that the remote branch has been deleted.

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