0

Is there some way to discover all branches I have in GitBash?

I want to discover and delete them.

Ry-
  • 199,309
  • 51
  • 404
  • 420
Caroline
  • 21
  • 3
  • Possible duplicate of [Can I delete all the local branches except the current one?](https://stackoverflow.com/questions/28572293/can-i-delete-all-the-local-branches-except-the-current-one) – ik1ne Sep 24 '19 at 02:05

2 Answers2

2

When you are in a repository if you run the command:

git branch

It will list all of your local branches you have for the repo. If you run:

git branch -a

It will list all of the your local branches as well as all of the remote branches available to be checked out. If the list is lengthy you will notice that your command line becomes a single colon, ":". This means that as you press the down key you can keep scrolling through all of the available branches. To exit this, just enter in q.

1

Local branches (shown by default with git branch)

The best way to proceed of course depends on the situation, and often you'll need to inspect each branch to make a decision.

# different ways to check branches, depending on your needs/tastes
git branch -v
git show-branch

...and on top of that git for-each-ref and custom formats for the output.

For the deletion part, for single branch treatment, you have

# delete a branch (with the safety check for unmerged commits)
git branch -d some_branch

# delete a branch (WITHOUT the safety check for unmerged commits)
git branch -D some_branch

However, if you need to delete ALL branches in batch mode, when you're sure of what you're doing, you can have an alias for that kind of purpose. Beware, though, to be sure to use that only as a wiping mechanism to delete every merged branch. (so every branch which are fully merged in their remote counterpart, or in HEAD for pure local branches with no upstream)

# to create an all-local-branches wiping alias
git config --global alias.wipe 'git branch -d $(git for-each-ref --format="%(refname:short)" refs/heads)'

then just

git wipe

It won't delete unmerged branches (because of -d) but it's often a good thing. Difficult to know for sure here, I didn't dare assume it since we don't a lot about your context, so feel free to comment if it's not useful in your case (if you need to inspect branches one at a time) and I'll just remove the off-topic part.

Remote branches (shown with git branch -r)

Remote branches are deleted with a request sent to the remote repo to delete the reference, along several possible forms, but mainly :

git push --delete origin some_branch
# or alternatively
git push origin :some_branch

Although, depending on your remote repository settings, it might be accepted or not.

RomainValeri
  • 14,254
  • 2
  • 22
  • 39