git 删除本地和远程分支
In most cases, it is simple to delete a Git branch. You'll learn how to delete a Git brach locally and remotely in this article.
在大多数情况下,删除Git分支很简单。 您将在本文中学习如何在本地和远程删除Git分支。
TL; DR版本 (TL;DR version)
// delete branch locally
git branch -d localBranchName
// delete branch remotely
git push origin --delete remoteBranchName
何时删除分支 (When to Delete branches)
It is common for a Git repo to have different branches. They are a great way to work on different features and fixes while isolating the new code from the main codebase.
Git回购通常有不同的分支。 在将新代码与主代码库隔离的同时,它们是处理不同功能和修复的好方法。
Repos often have a master
branch for the main codebase and developers create other branches to work on different features.
Repos通常为主要代码库提供一个master
分支,而开发人员创建其他分支以使用不同的功能。
Once work is completed on a feature, it is often recommended to delete the branch.
功能上的工作完成后,通常建议删除分支。
局部删除分支 (Deleting a branch LOCALLY)
Git will not let you delete the branch you are currently on so you must make sure to checkout a branch that you are NOT deleting. For example: git checkout master
Git不允许您删除当前所在的分支,因此必须确保签出未删除的分支。 例如: git checkout master
Delete a branch with git branch -d <branch>
.
使用git branch -d <branch>
删除一个分支。
For example: git branch -d fix/authentication
例如: git branch -d fix/authentication
The -d
option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D
instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.
-d
选项仅在分支已被推送并与远程分支合并后才删除。 如果要强制删除分支(即使尚未推送或合并),请使用-D
代替。
The branch is now deleted locally.
现在,该分支已在本地删除。
删除分支REMOTELY (Deleting a branch REMOTELY)
Here's the command to delete a branch remotely: git push <remote> --delete <branch>
.
这是远程删除分支的命令: git push <remote> --delete <branch>
。
For example: git push origin --delete fix/authentication
例如: git push origin --delete fix/authentication
The branch is now deleted remotely.
现在,该分支已被远程删除。
You can also use this shorter command to delete a branch remotely: git push <remote> :<branch>
您还可以使用以下较短的命令远程删除分支: git push <remote> :<branch>
For example: git push origin :fix/authentication
例如: git push origin :fix/authentication
If you get the error below, it may mean that someone else has already deleted the branch.
如果您看到以下错误,则可能意味着其他人已经删除了该分支。
error: unable to push to unqualified destination: remoteBranchName The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to 'git@repository_name'
Try to synchronize your branch list using:
尝试使用以下方法同步分支列表:
git fetch -p
The -p
flag means "prune". After fetching, branches which no longer exist on the remote will be deleted.
-p
标志的意思是“修剪”。 提取后,将删除远程上不再存在的分支。
翻译自: https://www.freecodecamp.org/news/how-to-delete-a-git-branch-both-locally-and-remotely/
git 删除本地和远程分支