We work hard to create and add new features to our software by working with new Git branches. But after some time it can be quite a local or remote branch hell. We can remove or delete local and remote branches in different ways.
通过与新的Git分支合作,我们努力创建并向我们的软件添加新功能。 但是一段时间后,它可能会变成本地或远程分支。 我们可以通过不同的方式删除或删除本地和远程分支。
切换到另一个分支 (Switch To Another Branch)
Before deleting a local branch we should change to the other branch. Generally, with a working branch, we may be using the local branch we want to delete. So as a first step we will switch to the new branch so we can delete the local branch.
在删除本地分支之前,我们应更改为另一个分支。 通常,对于工作中的分支,我们可能正在使用要删除的本地分支。 因此,第一步,我们将切换到新分支,以便我们删除本地分支。
We will print existing branches and currently active branch with like below.
我们将打印现有分支和当前活动分支,如下所示。
$ git branch
Then we are currently on the branch named newversion
and in order to delete this local branch, we will change to branch master
. We will use checkout
command and provide the branch name.
然后,我们当前在名为newversion
的分支上,并且要删除此本地分支,我们将更改为master
。 我们将使用checkout
命令并提供分支名称。
$ git checkout master
删除本地Git分支(Delete Local Git Branch)
Now we can delete or remove our branch with the option -d
or --delete
. -d
is the short version of the --delete
. We will also specify the local branch we want to delete. In this example, we will delete the branch named newversion
.
现在,我们可以使用-d
或--delete
选项删除或删除我们的分支。 -d
是--delete
的缩写。 我们还将指定我们要删除的本地分支。 在此示例中,我们将删除名为newversion
的分支。
$ git branch -d newversion
强制删除具有未合并更改的本地Git分支(Force To Delete Local Git Branch with Unmerged Changes)
If there are some minor problems like unmerged changes and the delete operation should be forced we can use -D
or --delete --force
. Actually -D
is an alias for the --delete --force
. In this example, we will forcibly delete the branch named com_support
.
如果有一些小问题,例如未合并的更改,并且应该强制执行删除操作,我们可以使用-D
或--delete --force
。 实际上-D
是--delete --force
的别名。 在此示例中,我们将强制删除名为com_support
的分支。
$ git branch -D com_support
删除远程存储库中不存在的所有本地分支(Delete All Local Branchs Not Presents In The Remote Repository)
While working in a big project we can stick to the remote branches. In some cases, we may need to remove/delete local branches that do not exist on the remote repository branches. We will use the following command which will delete/remove all local branches that do not exist in the remote repository branches.
在大型项目中工作时,我们可以坚持到远程分支机构。 在某些情况下,我们可能需要删除/删除远程存储库分支上不存在的本地分支。 我们将使用以下命令,该命令将删除/删除远程存储库分支中不存在的所有本地分支。
$ git branch -vv | grep ': gone]'| grep -v "\*" | \
awk '{ print $1; }' | xargs -r git branch -d
删除除主控外的所有本地分支 (Delete All Local Branches Except Master)
We can also use the following command in order to delete all local branches except the master branch. This will clean our home easily without much effort.
我们还可以使用以下命令来删除除master分支以外的所有本地分支。 这将轻松地清洁我们的房屋,而无需付出太多努力。
$ git branch | grep -v "master" | xargs git branch -D
删除远程Git分支(Delete Remote Git Branch)
We can also delete remove branches with the push
command. Because deleting a local branch won’t delete/remove the remote branch. In order to delete/remove the branch, we will also provide the remote repo name with the branch name we want to delete. We provide the repository name as git may work in a distributed manner where it can have multiple repositories. But generally, the remote repository will be origin
. In this example, we will delete the branch named test
from the remote repository named origin
.
我们还可以使用push
命令删除remove分支。 因为删除本地分支不会删除/删除远程分支。 为了删除/删除分支,我们还将为远程回购名称提供我们要删除的分支名称。 我们提供存储库名称,因为git可以以分布式方式工作,其中可以有多个存储库。 但通常,远程存储库将是origin
。 在此示例中,我们将从名为origin
的远程存储库中删除名为test
的分支。
$ git push origin --delete test
OR
要么
$ git push origin -D test
将分支列表与远程存储库和最后的分支同步 (Synchronize Branch List with The Remote Repository and Branches At The End)
At the end of the local or remote branches, removal/deletion synchronizing is a best practice. We will use the fetch
git command with the -p
option which will remove any remove-tracking references that no longer exist.
在本地或远程分支的末端,删除/删除同步是最佳实践。 我们将使用带有-p
选项的fetch
git命令,该命令将删除所有不再存在的删除跟踪引用。
$ git fetch -p
翻译自: https://www.poftut.com/how-to-delete-remove-git-branch-local-and-remote-tutorial-with-examples/