Deleting local and remote Git branches
Git comes into play in version control systems when you are working with a team and the entire team is making changes to the same code repository. Creating a new branch in Git is relatively easy than other version control systems and deleting a local branch in Git is no longer a big deal.
This article will show you how to delete a local or remote branch from Git. In most cases, when you first start working with any repository, the remote name of the repository is origin
. So, if you pushed code to origin
a branch, you must origin
delete it from the branch. We have two types of branches in any version control system. Local branches are where each team member manages their work on their computer, and remote branches are where all team members work simultaneously.
Deleting Git local branches
First, we will see how to delete a branch from our local personal computer using the command line. To delete a local branch from our personal computer in Git, we have to run on the command line in the project directory.
git branch -d <branch-name>
In the above example, -d
the parameter is used as an alias for delete, which is used to delete a specific branch if it has been fully merged with the remote branch. However, if you want to force delete a branch, then -D
use as an alias for force delete, which will delete the branch regardless of whether it has been merged in the remote branch or not.
Even if a branch has unmerged changes, Git will refuse to delete it. If you want to force this, you must force this deletion by -d
replacing the parameter with an uppercase one:-D
git branch -D <branch-name>
We can summarize local branches like this.
git branch --delete <branch>
git branch -d <branch> # Shorter version
git branch -D <branch> # Force-delete un-merged branches
Deleting a Git remote branch
As mentioned above, we use Git for local branches, but the situation is different for remote branches. We use git push
git for remote branches, even if it sounds a bit strange. In other words, we can say that we are pushing-sending-deleting branches to the remote repository. Here's how you do it:
git push --delete <remote branch name> <branch name>
In most cases, the remote name is origin
In some cases, you have to use the following command.
git push -d origin <branch_name>
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.
Related Articles
Changing drives in Git Bash
Publish Date:2025/03/30 Views:56 Category:Git
-
This short article will discuss how we can use Git Bash to have a Unix-style command line environment in Windows operating system and run multiple commands in this terminal. Git Bash Git is a collection of command-line utilities created to
Adding a remote branch in Git
Publish Date:2025/03/30 Views:142 Category:Git
-
Git does not allow its developers to create new branches on remote repositories. But instead, we can push an already existing local branch, and after doing so, we can bring it to the remote repository using some Git commands. In every versi
Synchronize your local repository with a remote repository in Git
Publish Date:2025/03/30 Views:92 Category:Git
-
This article outlines the process of syncing your local repository with a remote repository. We will also see how to sync a GitHub branch with a remote repository on the command line. Synchronize your local repository with a remote reposito
Creating a remote repository from a local repository in Git
Publish Date:2025/03/30 Views:105 Category:Git
-
This article discusses the necessary steps to create a remote repository based on a local repository. This is ideal when you have a local repository that needs to be available on a remote or SSH-enabled server. Creating a remote repository
Removing the upstream repository in Git
Publish Date:2025/03/30 Views:177 Category:Git
-
This article will teach you how to delete an upstream repository in Git. We may sometimes need to delete or change the remote repository that we use with our local repository. To do this, we can use the Git command git remote . Removing the
Git remote add SSH
Publish Date:2025/03/30 Views:53 Category:Git
-
In this day and age, the most widely used version control system is Git, which is operated by most developers within a team structure. This is mainly used to increase code efficiency, no matter how big or critical the project is. In this se
Differences between Git Reset, Revert and Checkout commands
Publish Date:2025/03/30 Views:185 Category:Git
-
This article discusses the differences between the git reset , , git revert and git checkout commands. These are some of the most useful Git utilities that allow us to undo certain changes in our repository. It’s easy to get confused with
Git revert local commits
Publish Date:2025/03/30 Views:84 Category:Git
-
When a software engineer or a web developer uses Git, it is obvious that he pushes a lot of code and commits to the Git repository every day, and while doing so, the need to undo or revert a particular commit or a set of commits arises from
Creating and using branches in Git
Publish Date:2025/03/30 Views:99 Category:Git
-
This article introduces Git branches. We will see how Git branches can help you organize your projects. Some of the commands we will deal with are git branch and git checkout . git branch Use commands to create, display, and delete branches