Git Crash Course #14 - Deleting Branches

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·10mo ago

Key Takeaways

The video demonstrates how to delete branches in Git, including how to delete a merged branch and a branch with unmerged commits, using the git branch command with the -d and -D flags.

Full Transcript

Okay then. So, we've just done our first merge and now we've got our newsletter feature integrated into the main branch. But if we look at our branches, we've still got that feature newsletter branch hanging around. We've also got the old my new branch that I created earlier as a test. And generally speaking, it's a good practice to clean up branches once you're done with them so they're not sitting around causing any kind of clutter. And I speak from experience when I say that having tens of branches lying around makes things a little confusing. you start to lose track of what's active work, what's not, and just having loads of stale branches makes it harder to stay organized. And this becomes even more true when you start to work in a team of developers all working on their own branches. So, in this lesson, we're going to see how to delete all branches when we don't need them anymore. And it's pretty simple to do. So, let's start with this newsletter feature branch, then since we already merged it into the main one, and we don't need it anymore. Now, importantly, in order to do this, we first of all need to make sure we're not currently on the branch we want to delete. I'm on main, so that's okay. But if you're on the feature branch, use the switch command to switch back to main before you try and delete the feature branch. So then now let's try deleting it by running the command get branch and then the hyphen d flag, which stands for delete. Then the name of the branch we want to delete, which is feature- newsletter. And once we run that, it's going to go ahead and delete that branch for us. Now, you can verify that by running the get branch command again to see all the current branches that you have. And you're going to see now that the feature one isn't there. Awesome. So then now let's try deleting the other branch that we don't need anymore. Now before we do that, I'm going to switch over to it by running the command get switch and then the name of the branch, which is my new branch. Not so new anymore. Okay. And this branch is now actually behind the main one because we made it back before we merged the newsletter feature into main. So before we delete this branch, I want to show you another pattern you might follow, which is to make a branch, start working on it, make a commit or two, and then maybe decide that you don't want that work anymore or you don't want that branch anymore. And we don't merge it back into main or anything. We just want to delete it. So then I'm literally just going to open the homepage file and then add a character into the title or something just so we've got something to commit. I'm going to save the file and then we want to add the change to the staging area by running the git add command then a dot and then press enter. Now I'll commit that change by running git commit along with the m flag to add a commit message which can be something I don't know like update homepage title. Okay. and then we can run it. So now we have a commit in this branch and now let's say we don't like it anymore. The feature is complete trash. We want to get rid of it. Well, let's do that. The first step again is to jump off the branch because we can't delete a branch if we're standing on it. So let's switch to the main branch by running the get switch command followed by main to jump to the main branch. And now let's try deleting that other branch that we just jumped off by running that same command as before, which was get branch. And then we use the hyphen d flag to delete one followed by the branch name which is my new branch. Now when we try to run this, git won't let us and it says instead that the branch is not merged. So this is git's way of protecting us from accidentally deleting a branch that we might not want to do and losing work essentially because that branch does have a commit on it now which hasn't been merged yet. So when we want to delete a branch with commits on it which haven't been merged, we need to change the delete command just a little bit to force the delete and all we need to do is change the small case lower D flag to an uppercase one. And that's you basically telling it look I know there's work on this branch that hasn't been merged but I still want to delete it. So let's run this command then. It's get branch and then this time hyphen D but uppercase D and then the branch name which is my new branch. And when we try to delete it this time it works. Git will delete that branch. And we can confirm that by running git branch and hitting enter to show us only the main branch is left. Okay. So that is how to delete branches. And again, it's always a good practice to get into when you're finished work on a branch so that your repo doesn't turn into a graveyard of old work and old branches. All right, then my friends. So, that is the end of the git crash course and the end of the preview of the whole Git and GitHub masterclass course that I released on the net.dev site. So if you want to carry this on and there is about another 5 hours of content all about Git advanced features uh GitHub using AI with GitHub as well the GitHub CLI then feel free to go and grab this course on the netinja.dev website. I will leave a link to the course page down below. It's $10 to buy or you can use the promo code which is get40 to get it for $6. And you can also sign up for a Net Ninja Pro subscription by clicking on this link right here. And that's going to be $9 a month to get access to all of my masterclass courses, including this one. And also, you get your first month half price. So, I really hope you've enjoyed this series so far. If you have, please do not forget to share, subscribe, and like. That really means a lot. And hopefully, I'm going to see you on the full one soon. Heat. Heat. N.

Original Description

In this Git tutorial series, you'll learn how to get up and running with Git in your own projects. You'll learn how to setup a repository, stage & commit changes, revert changes and how to use branches to work on new features. 🔥🥷🏼 Get instant access to the WHOLE Git & GitHub Masterclass: https://netninja.dev/p/git-github-masterclass 🔥🥷🏼 Get instant access to ALL premium courses on NetNinja.dev: https://netninja.dev/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video teaches how to delete branches in Git, including how to handle merged and unmerged branches, and how to use the git branch command with the -d and -D flags.

Key Takeaways
  1. Check if you are on the branch you want to delete
  2. Switch to a different branch using git switch
  3. Use git branch -d to delete a merged branch
  4. Use git branch -D to force delete a branch with unmerged commits
  5. Verify the branch has been deleted using git branch
💡 Git protects you from accidentally deleting a branch with unmerged commits by requiring the use of the -D flag to force the deletion.

Related Reads

📰
I Did the Math on Claude Opus 5: Max Effort Cost 94% More Than High Effort
Understand how effort levels impact costs in Claude Opus 5, a crucial factor for budgeting and resource allocation
Dev.to · tokenmixai
📰
I Banned AI Tools From My Creative Process for 30 Days. Here’s What Happened to My Brain
A creator banned AI tools from their process for 30 days and observed significant changes in their thinking and creativity, highlighting the impact of AI on human cognition
Medium · AI
📰
Best AI Tools for Content Creators With Zero Budget
Discover the best free AI tools for content creators with no budget, and learn how to leverage them for high-quality content
Medium · AI
📰
Stop Building AI Chatbots. Build These 7 AI Products Instead.
Diversify your AI portfolio by building products that solve real-world problems beyond chatbots, attracting clients and generating revenue
Medium · Machine Learning
Up next
How to Remove the Background of a Picture for Free with AI in 2026
TheFigCo
Watch →