Skip to content

How to Delete Local and Remote Git Branches

How to Delete Git Remote and Local Branch. Delete branch in git repository on local and remote system.

Steps
5
Covers
Git
Versions & support
About Git →
On this page 5 steps

Working with Git, you often create multiple branches to manage different features or stages of your project. However, as you merge these branches or they become obsolete, it’s essential to clean them up. This tutorial will guide you through the process of deleting local and remote Git branches.

Prerequisites

  • Basic understanding of Git commands
  • Git installed on your system
  • Access to a Git repository

Step 1 of 5List All Your Branches

Before deleting any branches, it’s a good idea to see a list of all existing branches.

  • Command: git branch
  • Purpose: Lists all local branches
  • Example:
    git branch 
    

Step 2 of 5Delete a Local Branch

To delete a local branch, you need to make sure you are not currently on the branch you want to delete.

  • Command: git branch -d <branch_name>
  • Purpose: Deletes the specified branch safely (if it’s fully merged)
  • Example:
    git branch -d feature/login 
    

Also you can force to delete branch

  • Command: git branch -D <branch_name>
  • Purpose: Forces deletion of the branch, regardless of its merge status
  • Example:
    git branch -D feature/obsolete 
    

Step 3 of 5Delete a Remote Branch

To delete a branch on a remote repository (like GitHub, GitLab, etc.), use the push command with the --delete flag.

  • Command: git push –delete <branch_name>
  • Purpose: Deletes the specified branch from the remote repository
  • Example:
    git push origin --delete feature/login 
    

Step 4 of 5Prune Local Tracking Branches

After deleting remote branches, you might still have local tracking branches. To clean these up, use the --prune option.

  • Command: git fetch –prune <branch_name>
  • Purpose: Removes local tracking branches that no longer exist on the remote
  • Example:
    git fetch --prune origin 
    

Step 5 of 5Verify Deletion

To ensure that your branches are deleted, list the branches again.

  • Local Check: git branch
  • Remote Check: git branch -r

Conclusion

Regularly deleting old branches helps keep your repository clean and manageable. Remember to double-check branch statuses and ensure you’re not deleting anything vital to your project.

Additional Tips

  • Always confirm the merge status of a branch before deleting it.
  • Use `git branch -a` to see both local and remote branches.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top