• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Delete Local Git Branch

Thumbnail for Philip WilkinsonPhilip Wilkinson

Philip Wilkinson

Software Engineer, Amazon

Published: 11/30/2023

About Terminus

Git’s branch features allow developers to work on different parts of a project simultaneously. However, once a branch has served its purpose, it is often helpful to delete it to keep the repository clean and organized.

Using Warp’s Workflow features

If you are using Warp as your terminal and you want to quickly retrieve the command to delete a local branch, you can use Warp’s Workflows feature by pressing CTRL-SHIFT-R, typing delete local git branch, then pressing ENTER to use the suggested command:

Thumbnail for

Delete a local branch

  1. 1. Start by checking the branch status

Before deleting a local branch, you should make sure that you are not on the branch you want to delete. To check the current branch status you can use the command:

 $ git branch

Run in Warp

This will display a list of all the local branches and the current branch will be marked with an asterisk (*).

  1. 1. Switch to a different branch

If you are on the branch you want to delete then you will need to switch to a different branch using the command:

 $ git checkout <branch-name>

Run in Warp

You will often want to checkout the main branch at this point so you can replace <branch-name> with main.

  1. 1. Delete the local branch

Now that you are on a different branch you can safely delete the local branch using the command:

 $ git branch -d <branch-name>

Run in Warp

This -d flag is an alias for --delete which can also be used.

  1. 1. Verify the branch has been deleted

To verify that the local branch has been deleted then you can use the git branch command again to list all the active branches. You should thus see the deleted branch removed from the list.

Delete a local branch with unmerged changes

If the branch has changes that haven’t been merged with another branch or pushed to a remote repository, Git will throw an error message to protect you from losing important commit data. If you want to delete this branch regardless though you can use the `-D` flag which will force the deletion of the branch:

 $ git branch -D <branch-name>

Run in Warp

This -D option is an alias for --delete --force.

Delete all local branches

In some workflows, you may forget to delete branches after you have been merged into the main branch. This can leave you with many branches that you want to delete all at once. 

The git branch -d command can delete more than one branch at once, so you can pass in multiple branch names:

 $ git branch -d <branch-name-1> <branch-name-2> <branch-name-3>

Run in Warp

Alternatively, if you want to delete all branches other than main, you can use regex to identify the branch you don’t want to delete whilst deleting all the others:

 $ git branch | grep -v “main” | xargs git branch -D

Run in Warp

Where grep -v finds all branch names that do not contain “main”, which are then passed to the git branch -D command.

Delete a local remote-tracking branch

A local remote-tracking branch is a reference to a branch on a remote repository that allows you to keep up to date with the changes on that remote branch. In some cases, you may not be interested in tracking the changes made in that remote branch, so you can stop your local repository from tracking those changes. To remove a specific remote-tracking branch you can use the --remotes or -r flag to delete the branch.

 $ git branch --delete --remotes origin/<remote-branch-name>
 $ git branch -dr origin/<remote-branch-name>

Run in Warp

Of if you want to remove all remote-tracking branches you can do so with the git fetch command, while the --prune or -p option is enabled:

 $ git fetch origin --prune
 $ git fetch origin -p

Run in Warp

Which remotes all obsolete local remote-tracking branches for any remote branch that no longer exists on the remote.

Written by

Thumbnail for Philip WilkinsonPhilip Wilkinson

Philip Wilkinson

Software Engineer, Amazon

Filed Under

Related Articles

Undo A Git Pull

How to effectively remove the commits introduced by a pull in Git using git-reset and preserve your local changes using git-stash. Also, how to cancel an unmerged pull request on GitHub.

Git
Thumbnail for Glory KimGlory Kim

Undo a Git Merge

How to rollback the changes introduced by a merge in Git by adding new opposite commits using git-revert and effectively removing commits using git-reset.

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

Prompt Show Git Branch In Prompt

Enhance your terminal with a custom Git prompt. Learn different ways to integrate this contextual info, from custom shell functions to Warp context chips and toolkits like Starship and P10K.

Git
Thumbnail for Gabriel ManricksGabriel Manricks

How To Remove Secrets From The Git History Remove Secrets From The Git History

Learn how to remove secrets from the Git history using the BFG and git-filter-repo command-line tools.

Git
Thumbnail for Utsav PoudelUtsav Poudel

Adding a Submodule in Git

This post will show you how to simply add a submodule to a local repository, clone a repository with a submodule, and work within a repository that has a submodule.

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

Undo a git push

This post will show you had to simply undo a git push three different ways.

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

Undo Git Add

Learn how to effectively use 'git add' to stage files in Git for committing, and discover two powerful methods to undo accidental stagings.

Git
Thumbnail for Glory KimGlory Kim

Undo a Git Rebase

This post will show you how to undo a rebase using git reset, git rebase and git revert

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

Git Push Origin

A breakdown of git push origin

Git
Thumbnail for Amanda KhooAmanda Khoo

Create Folder In GitHub Repository

Learn how to create and push one or more empty directories in a Git repository using `.placeholder` and `README.md` files using both the CLI and the GitHub interface.

Git
Thumbnail for Razvan LudosanuRazvan Ludosanu

Git Push Tags

This post will show you how to push a single tag, multiple tags, all tags, and tags with commits.

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

Undoing Git Commits

Explore ways to undo a commit, including git reset, git checkout, and git revert with git while preserving commit history.

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null