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

/ #Git
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)

References

Top Articles
Lake Nokomis, Wisconsin | Lake, Fishing & Travel Info
Discover the Best Parts of Minneapolis' Nokomis Neighborhood
Kostner Wingback Bed
Davita Internet
How Much Is 10000 Nickels
Craigslist Mexico Cancun
The Best Classes in WoW War Within - Best Class in 11.0.2 | Dving Guides
Yi Asian Chinese Union
Umn Pay Calendar
When Is the Best Time To Buy an RV?
Does Publix Have Sephora Gift Cards
[PDF] INFORMATION BROCHURE - Free Download PDF
Conduent Connect Feps Login
Cvs Learnet Modules
UEQ - User Experience Questionnaire: UX Testing schnell und einfach
Industry Talk: Im Gespräch mit den Machern von Magicseaweed
Transfer Credits Uncc
Truck Trader Pennsylvania
Ostateillustrated Com Message Boards
London Ups Store
Sonic Fan Games Hq
Pretend Newlyweds Nikubou Maranoshin
Iroquois Amphitheater Louisville Ky Seating Chart
Toyota Camry Hybrid Long Term Review: A Big Luxury Sedan With Hatchback Efficiency
Putin advierte que si se permite a Ucrania usar misiles de largo alcance, los países de la OTAN estarán en guerra con Rusia - BBC News Mundo
Yisd Home Access Center
Filthy Rich Boys (Rich Boys Of Burberry Prep #1) - C.M. Stunich [PDF] | Online Book Share
Riversweeps Admin Login
Churchill Downs Racing Entries
Mchoul Funeral Home Of Fishkill Inc. Services
Tu Housing Portal
Plasma Donation Racine Wi
Angel del Villar Net Worth | Wife
Housing Assistance Rental Assistance Program RAP
Dumb Money, la recensione: Paul Dano e quel film biografico sul caso GameStop
Babbychula
Mgm Virtual Roster Login
Gwu Apps
Laurin Funeral Home | Buried In Work
R Nba Fantasy
Craigslist Pets Huntsville Alabama
MSD Animal Health Hub: Nobivac® Rabies Q & A
VDJdb in 2019: database extension, new analysis infrastructure and a T-cell receptor motif compendium
Atu Bookstore Ozark
What is 'Breaking Bad' star Aaron Paul's Net Worth?
Bf273-11K-Cl
House For Sale On Trulia
Minute Clinic Mooresville Nc
Used Auto Parts in Houston 77013 | LKQ Pick Your Part
What your eye doctor knows about your health
Hkx File Compatibility Check Skyrim/Sse
Who We Are at Curt Landry Ministries
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 5789

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.