Git Delete Branch – How to Remove a Local or Remote Branch (2024)

/ #clean code
Git Delete Branch – How to Remove a Local or Remote Branch (1)
Dionysia Lemonaki
Git Delete Branch – How to Remove a Local or Remote Branch (2)

Git is a popular version control system and an essential tool in a web developer's toolkit.

Branches are a powerful and integral part of working with Git.

In this article, you will learn the basics about how to remove local and remote branches in Git.

What are Branches in Git?

A branch is a pointer to a commit.

Git branches are a snapshot of a project and its changes, from a specific point in time.

When working on a big project, there is the main repository with all the code, often called main or master.

Branching allows you to create new, independent versions of the original main working project. You might create a branch to edit it to make changes, to add a new feature, or to write a test when you're trying to fix a bug. And a new branch lets you do this without affecting the main code in any way.

So to sum up – branches let you make changes to the codebase without affecting the core code until you're absolutely ready to implement those changes.

This helps you keep the codebase clean and organized.

Why Remove Branches in Git?

So you've created a branch to hold the code for a change you wanted to make in your project.

You then incorporated that change or new feature into the original version of the project.

That means you no longer need to keep and use that branch, so it is a common best practice to delete it so it doesn't clutter up your code.

How to Delete a Local Branch in Git

Local branches are branches on your local machine and do not affect any remote branches.

The command to delete a local branch in Git is:

git branch -d local_branch_name
  • git branch is the command to delete a branch locally.
  • -d is a flag, an option to the command, and it's an alias for --delete. It denotes that you want to delete something, as the name suggests. - local_branch_name is the name of the branch you want to delete.

Let's look into this in a bit more detail with an example.

To list out all the local branches, you use the following command:

git branch

I have two, branches, master and test2. I am currently on the test2 branch as the (*) shows:

Git Delete Branch – How to Remove a Local or Remote Branch (3)

I want to delete the test2 branch, but it is not possible to delete a branch you are currently in and viewing.

If you try to do so, you'll get an error that will look something like this:

Git Delete Branch – How to Remove a Local or Remote Branch (4)

So before deleting a local branch, make sure to switch to another branch that you do NOT want to delete, with the git checkout command:

git checkout branch_name#where branch_name is the name of the branch you want to move to#in my case the other branch I have is master, so I'd do:#git checkout master

Here's the output:

Git Delete Branch – How to Remove a Local or Remote Branch (5)

Now I can delete the branch:

Git Delete Branch – How to Remove a Local or Remote Branch (6)

The command for deleting a local branch that we just used doesn't work in all cases.

If the branch contains unmerged changes and unpushed commits, the -d flag will not allow the local branch to be deleted.

This is because the commits are not seen by any other branches and Git is protecting you from accidentaly losing any commit data.

If you try to do this, Git will show you an error:

Git Delete Branch – How to Remove a Local or Remote Branch (7)

As the error suggests, you'll need to use the -D flag instead:

git branch -D local_branch_name

The -D flag, with a capital D (which is an alias for -- delete --force), forcefully deletes the local branch, regradless of its merged status.

But note that you should use this command should with caution, as there is no prompt asking you to confirm your actions.

Use it only when you are absolutely sure you want to delete a local branch.

If you didn't merge it into another local branch or push it to a remote branch in the codebase, you will risk losing any changes you've made.

Git Delete Branch – How to Remove a Local or Remote Branch (8)

How to Delete a Remote Branch in Git

Remote branches are separate from local branches.

They are repositories hosted on a remote server that can be accessed there. This is in comparison to local branches, which are repositories on your local system.

The command to delete a remote branch is:

git push remote_name -d remote_branch_name
  • Instead of using the git branch command that you use for local branches, you can delete a remote branch with the git push command.
  • Then you specify the name of the remote, which in most cases is origin.
  • -d is the flag for deleting, an alias for --delete.
  • remote_branch_name is the remote branch you want to delete.

Now, let's see an example of how to go about deleting a remote branch.

To view any remote branches, you use this command:

git branch -a

The -a flag (an alias for --all) shows all branches – both local and remote.

Git Delete Branch – How to Remove a Local or Remote Branch (9)

I have two local branches called master and test and two remote branches origin/master and origin/test.

The -r, an alias for --remotes, shows only the remote repositories.

Git Delete Branch – How to Remove a Local or Remote Branch (10)

I want to delete the remote origin/test branch, so I use the command:

git push origin -d test

Output:

Git Delete Branch – How to Remove a Local or Remote Branch (11)

This deleted the test branch in the remote repository named origin.

The origin/test remote repository is no longer there:

Git Delete Branch – How to Remove a Local or Remote Branch (12)

Conclusion

You now know how to delete local and remote branches in Git.

If you want to learn more about Git, you can watch the following courses on freeCodeCamp's YouTube channel:

Thanks for reading and happy learning!

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

Git Delete Branch – How to Remove a Local or Remote Branch (13)
Dionysia Lemonaki

Read more posts.

If this article was helpful, .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

Git Delete Branch – How to Remove a Local or Remote Branch (2024)

FAQs

Git Delete Branch – How to Remove a Local or Remote Branch? ›

How do I clean outdated branches? git fetch --prune is the best utility for cleaning outdated branches. It will connect to a shared remote repository remote and fetch all remote branch refs. It will then delete remote refs that are no longer in use on the remote repository.

How to delete local branch in bulk? ›

Bulk-deleting branches in Git
  1. List All Branches: Begin by listing the branches you want to delete. git branch.
  2. Filter and Delete: Use grep to filter branches and xargs to batch-delete. git branch | grep 'pattern' | xargs git branch -d.

How to prune local branches in git? ›

How do I clean outdated branches? git fetch --prune is the best utility for cleaning outdated branches. It will connect to a shared remote repository remote and fetch all remote branch refs. It will then delete remote refs that are no longer in use on the remote repository.

How to delete local git branch in VS Code? ›

Using Visual Studio Code (VS Code)

First, open VS Code, and then open your project folder in it. After confirming your selection, VS Code will execute Git's delete command and permanently remove your selected git branch from local repositories.

How to delete branch from both local and remote git? ›

To delete both a local and remote Git branch, even if the Git branch has the same name locally and remotely, two commands must be issued:
  1. A git push origin delete command deletes the remote Git branch.
  2. A git branch delete command deletes the local Git branch.
Aug 22, 2023

How do I force delete a local branch in git? ›

Delete the Local Branch: Git provides two options for deleting a branch: -d and -D . The -d option deletes a branch only if it has been merged into another branch. If it hasn't been merged, use the -D option, which forces the deletion.

What happens if I delete a branch locally? ›

On a technical level, a branch is only a pointer to a specific commit — a section of code you want to include in your main branch. Because of this, when you delete a local Git branch, the commits will still remain.

How to remove unused local branch git? ›

Once you are on a different branch, you can delete the local branch you no longer need. Use the 'git branch -d' or 'git branch -D' command. Here's the difference: 'git branch -d branch_name': This command will delete the branch only if it has been fully merged with the branch you are currently on.

How do I clean up a local git branch? ›

Local branches can help you organize your code and work on different tasks independently, but sometimes you may want to delete them after they are merged or abandoned. To delete local branches, you can use the git branch command with the -d or -D option.

How do I delete a git branch? ›

Deleting a Branch Locally

Run the Command: Use git branch -d branch_name to delete the branch. Replace branch_name with the actual name of the branch you wish to delete.

How do I remove remote branches that no longer exist? ›

Approach 1: Using git fetch -p

The -p (or –prune) option with git fetch will automatically remove any remote-tracking branches that no longer exist on the remote.

How to delete many local branches in git? ›

git branch -D <feature_branch_name>

You can also specify 1 or more branch names that you want to delete. The -D option is an alias for --delete --force i.e. -d -f, which will delete all specified branches regardless of whether the code changes have all been merged or not.

How to remove all local changes on git branch? ›

Discarding All Local Changes

If you want to undo all of your current changes, you can use the git restore command with the "." parameter (instead of specifying a file path): $ git restore . Again: please be careful with these commands! Once you've discarded your local changes, you won't be able to get them back!

How do I delete a local branch in git rider? ›

Delete branches

In the Branches popup or from the Branches pane of the Git tool window, right-click the branch you want to delete and choose Delete.

How do I delete unnecessary branches in git? ›

You cannot delete branches from GitHub's desktop apps or the hub terminal app. However, you can delete them using your terminal. Open Terminal: Navigate to your project directory. Run the Command: Use git branch -d branch_name to delete the branch.

How to delete all local branches in git with prefix? ›

git branch -D <feature_branch_name>

You can also specify 1 or more branch names that you want to delete. The -D option is an alias for --delete --force i.e. -d -f, which will delete all specified branches regardless of whether the code changes have all been merged or not.

How do I delete all merged branches locally? ›

3. Deleting all local branches that have been merged
  1. git branch --merged : Lists all merged branches.
  2. grep -v "^\*\\|main" : Filters out the main branch and the current branch (marked with an asterisk).
  3. xargs -n 1 git branch -d : Passes each branch name to git branch -d to delete it.

Top Articles
Momordica charantia L. | Plants of the World Online | Kew Science
September 30 is Going To Be A Sad Day for WWE 2K Fans
Extranet Landing Page Delta
Craigslist Apartments For Rent Cheap
Butte Jail Roster Butte Mt
ᐅ eGirl Kleidung kaufen: Wie ein eGirl aussehen so funktionierts!
Inside Watchland: The Franck Muller Watch Manufacturing Facilities | aBlogtoWatch
Sixth Circuit Denies Qualified Immunity for State University Officials Who Allegedly Violated Professor's First Amendment Rights
Sara Carter Fox News Photos
Morbus Castleman - Ursachen, Symptome & Behandlung
Nsu Kpcom Student Handbook
R Umineko
His Words Any Sense Figgerits
Hydro Quebec Power Outage Map
Who is Harriet Hageman, the Trump-backed candidate who beat Liz Cheney?
Ecolab Mppa Charges
Cassano's Pizza King Menu and Prices
Mobiloil Woodville Tx
Itawamba Ixl
102Km To Mph
A vintage funfair / fairground
Waifu Fighter F95
Retire Early Wsbtv.com Free Book
eUprava - About eUprava portal
Erome.ccom
Best 43-inch TVs in 2024: Tested and rated
Craigslist Mexico Cancun
Mcdonald's Near Me Dine In
715 Henry Ave
Broncos vs. Seahawks: How to Watch NFL Week 1 Online Today
Heiwa Coin
Rbgfe
Miawaiifu
Facebook Marketplace Winnipeg
Presentato il Brugal Maestro Reserva in Italia: l’eccellenza del rum dominicano
Lvpg Orthopedics And Sports Medicine Muhlenberg
Busty Bruce Lee
House Party 2023 Showtimes Near Mjr Chesterfield
Brian Lizer Life Below Zero Next Generation
Dan And Riya Net Worth In 2022: How Does They Make Money?
Cashflow Manager Avid
Alineaciones De Rcd Espanyol Contra Celta De Vigo
Doublelist Aiken Sc
Concord Mills Mall Store Directory
Splunk Stats Count By Hour
Jcp Meevo Com
QuiBids Review: Legit Penny Auction or a Scam? The Truth... - MoneyPantry
Disney Immersive Experience Cleveland Discount Code
Wush Ear Cleaner Commercial Actor
Six Broadway Wiki
Saybyebugs At Walmart
Love & Basketball streaming: where to watch online?
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 6277

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.