git 删除本地和远程分支_如何删除本地和远程git分支

git 删除本地和远程分支

If you have previously worked with Git for versioning your Angular code then there is a good chance that you have encountered a situation in which you wanted to delete a remote branch or multiple branches. This is a common occurrence for developers, particularly in large projects, so I wanted to write an article that explored the topic.

如果您以前曾与Git一起使用过,以对Angular代码进行版本控制,那么您很有可能遇到了要删除一个或多个远程分支的情况。 这对于开发人员来说是很常见的,尤其是在大型项目中,因此我想写一篇探讨该主题的文章。

In this article, we’ll learn:

在本文中,我们将学习:

  • How to delete a local branch in your Git repository

    如何在Git存储库中删除本地分支
  • How to delete a remote branch in Git

    如何在Git中删除远程分支
  • How to delete all Git branches which have been merged

    如何删除所有已合并的Git分支
  • How to remove all local branches, not on remote

    如何删除所有本地分支,而不是远程
  • How to delete all your local Git branches except master

    如何删除除master以外的所有本地Git分支

Before tackling how to delete a remote branch, we’ll first see how to delete a branch in the local Git repository.

在解决如何删除远程分支之前,我们将首先了解如何在本地Git存储库中删除分支。

Note: Version control systems are an indispensable tool in modern web development that can help you solve many issues related to numerous tasks. Git is one of the most popular version control systems nowadays.

注意: 版本控制系统是现代Web开发中必不可少的工具,可以帮助您解决与众多任务相关的许多问题。 Git是当今最受欢迎的版本控制系统之一。

Before we proceed to learn how to delete local and remote branches in Git, let’s define what a Git branch is and the side effects of deleting branches. A branch in Git is a pointer to a commit. If you delete a branch, it deletes the pointer to the commit. This means if you delete a branch that is not yet merged and the commits become unreachable by any other branch or tag, the Git garbage collection will eventually remove the unreachable commits.

在继续学习如何在Git中删除本地和远程分支之前 ,让我们定义一下Git分支是什么以及删除分支的副作用。 Git中的分支是指向提交的指针。 如果删除分支,它将删除指向提交的指针。 这意味着,如果删除尚未合并的分支,并且其他任何分支或标记都无法提交,则Git垃圾回收最终将删除不可达的提交。

删除本地分支 (Deleting Local Branches)

Let’s start by learning how to delete a local branch.

让我们开始学习如何删除本地分支。

  1. First, use the git branch -a command to display all branches (both local and remote).

    首先,使用git branch -a命令显示所有分支(本地和远程)。

  2. Next, you can delete the local branch, using the git branch -d command, followed by the name of the branch you want to delete.

    接下来,您可以使用git branch -d命令删除本地分支,后跟要删除的分支的名称。

$ git branch -a # *master # b1 # remote/origin/master # remote/origin/b1 
$ git branch -d b1 # Deleted branch b1.

Note: You can also use the -D flag which is equivalent to the --delete --force command instead of -d. This will enable you to delete the local branch regardless of its merge status.

注意: 您也可以使用 -D 标志,它等效于 --delete --force 命令,而不是 -d 这将使您可以删除本地分支,而不考虑其合并状态。

删除远程分支 (Deleting Remote Branches)

Unlike local branches, you can’t delete a remote branch using the git branch command. However, you need to use the git push --delete command, followed by the name of the branch you want to delete. You also need to specify the remote name (origin in this case) after git push.

与本地分支不同,您不能使用git branch命令删除远程分支。 但是,您需要使用git push --delete命令,后跟要删除的分支的名称。 您还需要在git push之后指定remote名称(在这种情况下为origin )。

$ git branch -a# *master
# b1
# remote/origin/master
# remote/origin/b1$ git push origin --delete b1
# [...]
# - [deleted] b1

如何删除所有非合并的Git分支? (How Can You Delete All Non-Merged Git Branches?)

Now that we have seen how can you delete local and remote branches in your Git repositories, let’s imagine that you have multiple Git branches. How can you delete the branches that have already been merged? Ideally, you would be able to do this all at once instead of deleting them branch by branch.

现在我们已经了解了如何删除Git存储库中的本地和远程分支,让我们假设您有多个Git分支。 如何删除已经合并的分支? 理想情况下,您可以一次完成所有操作,而不必逐个删除它们。

Note: Merging is performed using the git merge command and it simply means integrating changes from another branch.

注意 :合并是使用 git merge 命令执行的,它只是意味着集成来自另一个分支的更改。

First, you need to get all the branches that are merged in the remote repository using the following command:

首先,您需要使用以下命令获取在远程存储库中合并的所有分支:

$ git branch --merged

If you have one merged branch, you can simply delete the merged local branch using the following command:

如果您有一个合并分支,则可以使用以下命令简单地删除合并的本地分支:

$ git branch -d branch-name

If you want to delete it from the remote repository use the following command:

如果要从远程存储库中删除它,请使用以下命令:

$ git push --delete origin branch-name

删除所有本地分支,不在远程 (Remove All Local Branches, not on Remote)

You can remove all local branches that are not on the remote repository by using the following bash command:

您可以使用以下bash命令删除不在远程存储库上的所有本地分支:

$ git branch -r | egrep -v -f /dev/fd/0  <(git branch -vv | grep origin) | xargs git branch -d

Let’s break down this command:

让我们分解一下这个命令:

  1. First, we get all remote branches using the git branch -rcommand

    首先,我们使用git branch -r命令获取所有远程分支

  2. Next, we get the local branches not on the remote using theegrep -v -f /dev/fd/0 <(git branch -vv | grep origin) command,

    接下来,我们使用egrep -v -f /dev/fd/0 <(git branch -vv | grep origin)命令获得不在远程的本地分支,

  3. Finally, we delete the branches using the xargs git branch -d command.

    最后,我们使用xargs git branch -d命令删除分支。

grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. Its name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect. grep was originally developed for the Unix operating system, but later available for all Unix-like systems and some others such as OS-9.

grep 是一个命令行实用程序,用于在纯文本数据集中搜索与正则表达式匹配的行。 它的名称来自ed命令g / re / p(全局搜索正则表达式并打印匹配的行),其作用相同。 grep最初是为Unix操作系统开发的,但后来可用于所有类似Unix的系统以及某些其他操作系统,例如OS-9。

xargs (short for “eXtended ARGuments”) is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.

xargs (“扩展ARGuments”的缩写)是Unix和大多数类似Unix的操作系统上的命令,用于从标准输入生成和执行命令。 它将输入从标准输入转换为参数到命令。

删除除Master以外的所有本地Git分支 (Delete All Your Local Git Branches Except Master)

If you have finished with local Git branches, it’s usually a good practice to remove them to free their space. You can simply run the following command:

如果您已经完成了本地Git分支,通常最好删除它们以释放它们的空间。 您可以简单地运行以下命令:

$ git branch | grep -v "master" | xargs git branch -D

We use the grep -v "master" command to search for branches (not including the master branch) then we delete them using the git branch -D command.

我们使用grep -v "master"命令搜索分支(不包括master分支),然后使用git branch -D命令删除它们。

结论 (Conclusion)

Throughout this article, we’ve seen how you can delete remote and local branches from your Git repositories. We’ve learned:

在本文中,我们已经了解了如何从Git存储库中删除远程和本地分支。 我们了解到:

  • How to delete a local branch in your Git repository

    如何在Git存储库中删除本地分支
  • How to delete a remote branch in Git

    如何在Git中删除远程分支
  • How to delete all Git branches which have been merged

    如何删除所有已合并的Git分支
  • How to remove all local branches, not on remote

    如何删除所有本地分支,而不是远程
  • How to delete all your local Git branches except master

    如何删除除master以外的所有本地Git分支

Thank you for reading my article, I hope it has been helpful. This post was originally posted on Techiediaries.

感谢您阅读我的文章,希望对您有所帮助。 该帖子最初发布在Techiediaries上 。

翻译自: https://itnext.io/how-to-delete-local-and-remote-git-branches-b8ad8baaded6

git 删除本地和远程分支

你可能感兴趣的:(git,python,github,java)