Wake Me Up When September Ends.

A wanderer capable of grasping the beauty of the grass, with a heart full of ancient charm, and a fondness for playful wit. Those who understand my words are knowledgeable individuals; those who decipher my code truly comprehend the essence. I am a wandering code swordsman, carrying my skills and riding freely through the digital world.

🆒Git 批量删除本地以及远程分支

1. 查看所有本地分支

git branch

2. 删除本地分支

git branch -D branch_name

其中,branch_name为要删除的分支名称。

3. 查看所有远程分支

git branch -r

4. 删除远程分支

git push origin --delete branch_name

如果要批量删除本地分支,可以使用以下命令:

git branch | grep -v "main" | xargs git branch -D

其中,”main”为要保留的分支名称,可以根据需要进行修改。

如果要批量删除远程分支,可以使用以下命令:

git branch -r | grep -v "main" | sed 's/origin\//:/' |  xargs git push
# -v:反转匹配,只显示不匹配的行。

main为要保留的分支名称,可以根据需要进行修改。最后一个命令中的sed命令是将远程分支名中的origin/替换为:/,因为git push命令中需要使用的远程分支名不包含origin/

5. 删除远程分支已经删除,但本地仍然存在的分支

git remote prune origin

该命令会检查本地仓库中的远程分支引用,并将那些在远程仓库中已经不存在的分支引用删除掉,以保持本地仓库的分支列表与远程仓库保持同步。