2023-03-29 06:49:39 +00:00
|
|
|
|
---
|
2023-10-27 06:28:17 +00:00
|
|
|
|
title: 🆒Git 批量删除本地以及远程分支
|
2023-03-29 06:49:39 +00:00
|
|
|
|
date: 2023-03-29
|
|
|
|
|
tags: Git
|
|
|
|
|
---
|
|
|
|
|
|
2023-10-24 02:54:34 +00:00
|
|
|
|
### 1. 查看所有本地分支
|
2023-03-29 06:51:33 +00:00
|
|
|
|
|
2023-03-29 06:49:39 +00:00
|
|
|
|
```sh
|
|
|
|
|
git branch
|
|
|
|
|
```
|
|
|
|
|
|
2023-10-24 02:54:34 +00:00
|
|
|
|
### 2. 删除本地分支
|
2023-03-29 06:49:39 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
git branch -D branch_name
|
|
|
|
|
```
|
2023-10-24 02:54:34 +00:00
|
|
|
|
|
2023-03-29 06:49:39 +00:00
|
|
|
|
其中,branch_name为要删除的分支名称。
|
|
|
|
|
|
2023-10-24 02:54:34 +00:00
|
|
|
|
### 3. 查看所有远程分支
|
2023-03-29 06:49:39 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-10-24 02:54:34 +00:00
|
|
|
|
git branch -r
|
2023-03-29 06:49:39 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-10-24 02:54:34 +00:00
|
|
|
|
### 4. 删除远程分支
|
|
|
|
|
|
2023-03-29 06:49:39 +00:00
|
|
|
|
```shell
|
|
|
|
|
git push origin --delete branch_name
|
|
|
|
|
```
|
2023-10-24 02:54:34 +00:00
|
|
|
|
<!--more-->
|
2023-03-29 06:49:39 +00:00
|
|
|
|
> 如果要批量删除本地分支,可以使用以下命令:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
git branch | grep -v "main" | xargs git branch -D
|
|
|
|
|
```
|
2023-10-24 02:54:34 +00:00
|
|
|
|
|
2023-03-29 06:49:39 +00:00
|
|
|
|
其中,"main"为要保留的分支名称,可以根据需要进行修改。
|
|
|
|
|
|
|
|
|
|
如果要批量删除远程分支,可以使用以下命令:
|
2023-10-24 02:54:34 +00:00
|
|
|
|
|
2023-03-29 06:49:39 +00:00
|
|
|
|
```shell
|
|
|
|
|
git branch -r | grep -v "main" | sed 's/origin\//:/' | xargs git push
|
2023-12-12 01:55:45 +00:00
|
|
|
|
# -v:反转匹配,只显示不匹配的行。
|
2023-03-29 06:49:39 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-10-24 02:54:34 +00:00
|
|
|
|
`main`为要保留的分支名称,可以根据需要进行修改。最后一个命令中的sed命令是将远程分支名中的`origin/`替换为`:/`,因为git push命令中需要使用的远程分支名不包含`origin/`
|
2023-10-27 06:27:29 +00:00
|
|
|
|
|
|
|
|
|
### 5. 删除远程分支已经删除,但本地仍然存在的分支
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
git remote prune origin
|
|
|
|
|
```
|
2023-11-24 05:15:59 +00:00
|
|
|
|
|
2023-10-27 06:27:29 +00:00
|
|
|
|
该命令会检查本地仓库中的远程分支引用,并将那些在远程仓库中已经不存在的分支引用删除掉,以保持本地仓库的分支列表与远程仓库保持同步。
|