JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Deleting a branch in Git

Author:JIYIK Last Updated:2025/04/03 Views:

This article will explain how to delete local and remote branches in Git.

We create branches in Git to separate development work (i.e.) we can create a branch for a feature, separate from the main branch.

Sometimes, we may decide to discard a branch and remove it from the Git repository. The branch we want to delete may exist in the local or remote repository.

We will now illustrate this with an example.


Deleting branches in Git using git branchandgit push

We usually create multiple branches in a Git repository in a typical development environment, in addition to the master branch. We use the master branch for final production-level changes.

We might use one branch for feature development and another for bug fixes. Later, when we want to release, we merge these branches into the release or master branch.

Sometimes, we may decide to delete some branches that are no longer needed. Branches may exist only in the local repository of Git or in the remote repository.

Suppose we have a branch named , a local branch, in the Git repository feature1. To delete a local branch in Git, we use the command with -dthe option git branch.

The command syntax to delete a local branch is git branch -d <branch_name>. So, we use the following code to delete our local branch feature1.

$ git branch -d feature1

We can use the -d branch -Dand -d branch command git branch, which is --delete --forcean alias for the -d branch option. This will cause the branch to be deleted even if it has not been fully merged with its upstream branch.

So we can do this.

$ git branch -D feature1

Sometimes, we may also need to delete a remote branch. We may decide that we are done with a branch and it is no longer needed by the development team.

Therefore, we may no longer keep that branch in the remote repository.

We can delete the remote branch using the command with the -d --deleteoption .git push

The command syntax to delete a remote branch is git push <remote_name> --delete <branch_name>.

For example, branch feature1is a remote branch. We can delete the remote branch as follows.

$ git push origin --delete feature1

Therefore, we have explained in detail how to delete local and remote branches in Git.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Git shows remote tracking branches

Publish Date:2025/04/03 Views:125 Category:Git

Branches on remote Git repositories are called remote branches. These are pointers to our remote repositories, including branches, tags, etc. Local branches only exist on each developer's local personal computer, but there is only one remot

View merged and unmerged branches in Git

Publish Date:2025/04/03 Views:96 Category:Git

This article discusses how to list merged and unmerged branches in Git. Git branches encourage convergent evolution of code. This is where we create a branch as a temporary space to work on a feature, and then merge the branch with its orig

The difference between Fork and Branch on GitHub

Publish Date:2025/04/03 Views:157 Category:Git

This article discusses the difference between Form and Branch on GitHub. In the context of coding, the current era relies more on collaboration. GitHub is one of the most commonly used collaboration tools. Forking and branching on GitHub ar

How to determine the current branch in Git

Publish Date:2025/04/03 Views:164 Category:Git

Git is a unique and popular version control system that is used by most of the software developers to keep an eye on the changes made in various applications and stay connected with other teams on the running projects. It helps large teams

Difference between Git Merge Origin/Master and Git Pull

Publish Date:2025/04/03 Views:195 Category:Git

This article outlines the differences between the git merge origin/master and git pull commands. These two commands integrate changes from a remote repository into the current local branch. However, each command's operation is unique and ha

Git Pull Origin branch overwrites Master branch

Publish Date:2025/04/03 Views:143 Category:Git

This article explains how we can revert the changes made to the master git pull origin branch branch after running the command . Assume that you have a master branch and a feature branch in your local and remote repositories . You pull chan

Git Pull and Merge Commands

Publish Date:2025/04/03 Views:56 Category:Git

In the vast world of version control systems, Git is the only fast, well-organized, in-demand, and easily accessible version control system that enables users to work on various projects simultaneously in an efficient manner without any col

Rolling back to an old commit in a public Git repository

Publish Date:2025/04/03 Views:115 Category:Git

This article explains how we can roll back a public Git repository to an old commit. When using Git version control, we can go back to any desired point. Without further ado, let’s get into today’s agenda. Rolling back to an old commit

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial