• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

? How to Delete All Local Git Branches Except the Current One

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Working with Git for a while? Your local repository might be cluttered with old branches you no longer need.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



If you're tired of cleaning them one by one, here's a one-liner to delete all local Git branches except the one you're currently on. ?

✅ The Command


git branch | grep -v "$(git rev-parse --abbrev-ref HEAD)" | xargs git branch -D
? What It Does


Let's break it down step by step:

? git rev-parse --abbrev-ref HEAD


This gives you the name of the current branch you're on.


main
? git branch


Lists all local branches, like so:


feature/login
feature/profile
* main
? grep -v "$(git rev-parse --abbrev-ref HEAD)"


This removes (filters out) the current branch from the list. -v means "exclude matching lines".

? xargs git branch -D


This force-deletes (-D) each branch passed in from the previous output.

⚠ Warning


This command will force delete all local branches except your current one. That means:

  • Any unmerged changes in those branches will be lost.
  • This does not affect remote branches.
  • Use with caution.

If you want to be safe, replace -D with -d:


git branch | grep -v "$(git rev-parse --abbrev-ref HEAD)" | xargs git branch -d

This will only delete branches that have been merged into the current branch.

? Pro Tip: Make it an Alias


If you find yourself using this often, add it to your shell aliases:


alias git-clean-branches='git branch | grep -v "$(git rev-parse --abbrev-ref HEAD)" | xargs git branch -D'

Then just run:


git-clean-branches
? Summary


When you're done with old branches and want to keep your workspace clean, this one-liner saves you from deleting each branch manually.

No more:


git branch -d branch1
git branch -d branch2

Just one command and your local branches are tidy again. ?


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу