ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Getting Started with Git
Chapter 1: Introduction to Version Control and Git
What is Version Control?
Why Use Version Control Systems (VCS)?
Centralized vs Distributed VCS
Introducing Git: A Distributed Approach
Core Git Concepts: Repository, Commit, Branch
Installing Git on Windows
Installing Git on macOS
Installing Git on Linux
Initial Git Configuration (user.name, user.email)
Getting Help with Git Commands
Quiz for Chapter 1
Chapter 2: Your First Git Repository
Creating a New Repository with git init
The Git Workflow: Modified, Staged, Committed
Checking the Status of Your Files (git status)
Adding Files to the Staging Area (git add)
Committing Changes (git commit)
Writing Good Commit Messages
Viewing Commit History (git log)
Understanding .gitignore Files
Hands-on Practical: Initialize and Make First Commit
Quiz for Chapter 2
Chapter 3: Viewing History and Undoing Changes
Comparing Changes (git diff)
Viewing Changes Between Commits
Viewing Staged vs Unstaged Changes
Unstaging Files (git reset HEAD )
Amending the Last Commit (git commit --amend)
Reverting Commits (git revert)
Removing Files from Git (git rm)
Moving or Renaming Files (git mv)
Practice: Inspecting and Modifying History
Quiz for Chapter 3
Chapter 4: Branching and Merging Basics
What Are Branches in Git?
Creating a New Branch (git branch)
Switching Between Branches (git checkout or git switch)
Listing Branches
Making Commits on a Branch
Merging Branches (git merge)
Understanding Fast-Forward Merges
Handling Merge Conflicts
Deleting Branches (git branch -d)
Hands-on Practical: Branching and Merging Workflow
Quiz for Chapter 4
Chapter 5: Working with Remote Repositories
Introduction to Remote Repositories
Common Hosting Platforms (GitHub, GitLab, Bitbucket)
Adding a Remote Repository (git remote add)
Viewing Remote Repositories (git remote -v)
Cloning an Existing Repository (git clone)
Pushing Changes to a Remote (git push)
Fetching Changes from a Remote (git fetch)
Pulling Changes from a Remote (git pull)
Understanding origin and upstream
Practice: Cloning, Pushing, and Pulling
Quiz for Chapter 5

Deleting Branches (git branch -d)

Once the work you've done on a branch is successfully integrated into your main line of development (typically by merging it into a branch like main or master), the feature branch itself often becomes redundant. Keeping too many old branches around can clutter your repository view and make it harder to navigate. Git provides a simple command to clean up these completed branches.

Removing Local Branches

The primary command for deleting a local branch is git branch with the -d option (short for --delete):

$ git branch -d <branch-name>

Replace <branch-name> with the actual name of the branch you wish to remove.

For example, if you created a branch called fix-login-bug, made your commits, switched back to main, and successfully merged fix-login-bug into main using git merge fix-login-bug, you can then safely delete the fix-login-bug branch:

# Ensure you are not currently on the branch you want to delete
$ git switch main
Switched to branch 'main'

# Delete the merged branch
$ git branch -d fix-login-bug
Deleted branch fix-login-bug (was a1b2c3d).

The hash a1b2c3d shown in the output is the SHA-1 identifier of the last commit on the deleted branch. This confirms the deletion was successful.

Safety First: The -d Flag

Using the lowercase -d flag is the recommended way to delete branches because it includes a safety check. Git will prevent you from deleting a branch if it contains work (commits) that has not yet been merged into the branch you are currently on (or more generally, into any branch that is an ancestor of HEAD).

If you try to delete an unmerged branch using -d, Git will refuse and display an error message like this:

$ git branch -d experimental-feature
error: The branch 'experimental-feature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D experimental-feature'.

This safety feature helps prevent accidental loss of work. It forces you to double-check if the commits unique to that branch are truly no longer needed or if they should be merged first.

Forcing Deletion: The -D Flag

Sometimes, you might want to delete a branch even if it hasn't been merged. This could happen if you started an experimental feature, decided it wasn't going anywhere, and want to discard the work entirely.

In such cases, you can use the uppercase -D flag:

$ git branch -D <branch-name>

This command forcefully deletes the branch, regardless of its merge status.

$ git branch -D experimental-feature
Deleted branch experimental-feature (was e4f5g6h).

Use -D with caution. Since it bypasses the safety check, you could potentially lose commits if they only exist on the branch being deleted and haven't been merged elsewhere. Ensure you are certain you no longer need any of the changes on that branch before using git branch -D.

Verifying Deletion

After deleting a branch, you can verify its removal by listing your local branches again:

$ git branch
* main
  another-feature

Notice that the deleted branch (fix-login-bug or experimental-feature in our examples) no longer appears in the list.

Deleting Remote Branches

It's important to remember that git branch -d and git branch -D only delete the branch in your local repository. They do not affect any remote repositories (like those hosted on GitHub or GitLab). Deleting branches on remotes involves the git push command, which is typically covered when discussing remote repository interactions in more detail. For now, focus on keeping your local repository tidy by removing branches once they have served their purpose and their work has been safely integrated.

  • git-branch(1) Manual Page, Git Project, 2024 - Official documentation for the git branch command, describing its use for creating, listing, and deleting branches, including the -d and -D options.
  • Pro Git, Scott Chacon and Ben Straub, 2014 (Apress) - A widely respected book on Git, offering in-depth explanations of Git's branching model and common operations, such as local branch deletion.
  • Deleting a Branch, Atlassian, 2024 (Atlassian) - A practical guide explaining the process of deleting local and remote Git branches, suitable for understanding common workflows.

© 2026 ApX Machine Learning