如何在本地和远程删除/删除Git分支?

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
Switch To Another Branch
Switch To Another Branch
切换到另一个分支

删除本地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
Delete Local Git Branch
Delete Local Git Branch
删除本地Git分支

强制删除具有未合并更改的本地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
Force To Delete Local Git Branch with Unmerged Changes
Force To Delete Local Git Branch with Unmerged Changes
强制删除具有未合并更改的本地Git分支

删除远程存储库中不存在的所有本地分支(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
Delete All Local Branches Except Master
Delete All Local Branches Except Master
删除除主控外的所有本地分支

删除远程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
Synchronize Branch List with The Remote Repository and Branches At The End
Synchronize Branch List with The Remote Repository and Branches At The End
将分支列表与远程存储库和最后的分支同步
LEARN MORE  How To Create and Manage New Branch with Git?
了解更多如何使用Git创建和管理新分支?

翻译自: https://www.poftut.com/how-to-delete-remove-git-branch-local-and-remote-tutorial-with-examples/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值