Skip to main content

Command Palette

Search for a command to run...

How to remove a local Git branch

Published
1 min read
How to remove a local Git branch

Introduction

In this small article we'll look into the steps you can follow to delete a local Git branch.

Steps

To remove a local Git branch, follow these steps:

  • Switch to a different branch: You cannot delete a branch you are currently on. Use git checkout <another_branch_name> or git switch <another_branch_name> to move to a different branch (e.g., main or develop).
    git checkout main
  • Delete the local branch:

    • If the branch has been fully merged into your current branch: Use the -d (or --delete) flag. This is the safe way to delete branches as it prevents data loss from unmerged changes.
      git branch -d <branch_to_delete>
      
    • If the branch has unmerged changes (force delete): Use the -D flag. This will delete the branch even if it contains commits not merged into any other local branches or pushed to a remote. Use this with caution as it can lead to data loss if not handled carefully.
      git branch -D <branch_to_delete>
      
  • Verify the deletion: You can check if the branch has been successfully removed from your local repository by listing all local branches.

    git branch
    

    The deleted branch should no longer appear in the list.

Conclusion

And there you have it, a simply step by step guide to help you manage your Git branches.

More from this blog

Cloud Tuned

625 posts

Your starting point for anything cloud: AWS, Azure, GCP, Serverless, Architecture, Hybrid Cloud, Systems Design and other Information Technology topics.