Home >Development Tools >git >How to delete a branch in Git

How to delete a branch in Git

PHPz
PHPzOriginal
2023-04-03 09:18:1411177browse

In Git, a branch represents an independent development line and can be merged with the master branch. However, after development is complete, we may need to delete certain branches in order to keep the code base clean. This article will explain how to delete a branch in Git.

Delete local branch

In Git, we can use the following command to delete a local branch:

git branch -d <branch_name>

Where, <branch_name> is The name of the branch you want to delete. For example, if you want to delete the branch named "feature-01", you should enter the following command:

git branch -d feature-01

If there are unmerged modifications on the branch, Git will prompt you to confirm. If you want to force delete the branch, please use the following command instead:

git branch -D <branch_name>

Delete remote branch

If you want to delete a branch that has been pushed to the remote server, you can use the following command:

git push <remote_name> --delete <branch_name>

Among them, <remote_name> is the name of your remote warehouse (usually "origin"), <branch_name> is the branch you want to delete name. For example, if you want to delete the remote branch named "feature-01", you should enter the following command:

git push origin --delete feature-01

It is worth noting that you cannot delete the branch you are currently working on. If you try to delete a currently used branch, Git will give the following error message:

error: Cannot delete branch 'feature-01' checked out at '/path/to/repo'

In this case, you need to switch to another branch first and then delete the branch.

Summary

In this article, we introduced how to delete a branch in Git. If you want to delete a local branch, you can use git branch -d <branch_name> or git branch -D <branch_name>; if you want to delete a remote branch, you can use git push <remote_name> --delete <branch_name>. Whenever you do, make sure you actually need to delete the branch and that there are no important unmerged changes on the branch.

The above is the detailed content of How to delete a branch in Git. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:git how to forkNext article:git how to fork