Guillaume M
Guillaume M

Reputation: 510

IntelliJ - GIT delete local orphan branch in GUI

I use IntelliJ (2016.1.3) and GIT integration plugin (8.1). During my use of IntelliJ I checked out multiple branches. Now those branches are deleted from GIT (after each pull request we delete them).

From now on every branch I checked out once is present in my GIT view from Intellij (by VCS > Git > Branches...), see under :

enter image description here

There is a way to delete them : do it ... one by one ! But if there are a lot of branches (let say a hundred ...), it certainly getting tedious...

The question is : Do you know if there is a simple way to delete all the orphan local branches from this view in IntelliJ ?

Thanks :)

EDIT : I have found a way to delete them but in another tool : "Git Gui". When you delete it from there, it's deleted in IntelliJ too. And you can delete multiple branches at one time.

Upvotes: 15

Views: 27890

Answers (8)

Samurai Soul
Samurai Soul

Reputation: 245

From within Intellij, go to Git > Show Git Log to display all of your branches, both local and remote. Select the local branches you want to delete (ctrl-click or shift-click) and then hit the delete Delete button button on the left. It's not a single command, but it should be much easier than deleting them one by one.

Git Log example

Edit: After posting, I found this is also answered here with the same solution

Upvotes: 0

Morlin
Morlin

Reputation: 99

The Intellij-Plugin GitToolBox offers an option called "Git branches cleanup" which does just what you appear to need.

Once installed, go to Git -> GitToolBox -> Git branches cleanup and select those branches you wish to delete.

Upvotes: 4

sanjeevjha
sanjeevjha

Reputation: 1549

Another way to delete local git branch, which might be helpful to you as per your requirement:

git branch -d branch1 branch2 branch3 branch4

If you execute this command on the terminal from the project directory, it will delete all the branches that you have passed i.e. branch1 branch2 ... branch_n.

Upvotes: 2

Sandro L
Sandro L

Reputation: 1150

To make deletion of a lot of branches a little less painful you may go to the git tool window (Hotkey Alt + 9). In the branches panel it is possible to select multiple branches and delete them.

So you do not have to delete them one by one. But be aware to not delete branches that are still of interest as you are not able to see the connection to remote branches or if this is pruned in this view.

Upvotes: 5

Madhavi Ambati
Madhavi Ambati

Reputation: 69

Git tracks its local and remote repositories details in .git folder under the git project.

To remove local orphan branches, go to:

.git -> refs -> heads (in this location you see all your local branches)

Then delete the branches whith you do not need.

Note: If you are using mac os clear your trash bin as well.

This clears your local orphan branches in one shot.

Upvotes: 6

Chaithu Narayana
Chaithu Narayana

Reputation: 551

I tried the updated answer from @Guillaume M and works fine for me. I had lots of local branches (on my IntelliJ) which are hanging loose as their remote counterparts were merged and deleted remotely. There wasn't any housekeeping done over a period of time and the dangling references kept piling up. So, basically these are orphans not tracking any remote branches.

Here's what I did to clean them up - used 'Git Gui' to delete the orphan local branches and refresh/restart (not mandatory though) IntelliJ. Git gui appears slightly convenient for this kind of batch deletion of the branches.

  1. Launch 'Git Gui' tool
  2. 'Open Existing Repository'. Choose path to the repository that you are working with your IntelliJ IDE.
  3. On the top menu, choose 'Branch' -> 'Delete'
  4. A pop-up appears with all the local branches in your repository.
  5. Choose all the local branches that you want to delete and click 'Delete'. You may add preconditions to prevent any accidental deletions. In my case they were all loosely hanging references with no real knowledge about them being merged to some other branch. Hence I chose 'Do not perform merge checks' to proceed with the operation which otherwise would fail on the preconditions.

Hopefully, IntelliJ comes up with an option to automatically housekeep these kind of branches. Or it already exists and I'm missing it. In any case, this approach saved me some time. Delete local branches conveniently from git GUI

Upvotes: 1

Colin D Bennett
Colin D Bennett

Reputation: 12084

Remote tracking branches are easily confused with normal Git branches. A remote tracking branch begins with remotes/, as in remotes/origin/branch1, although Git allows you to omit remotes/ as a shorthand, so you can also refer to remotes/origin/branch1 as simply origin/branch1. But these are not normal branches. You can't commit to them or check them out directly. If you try to check them out, Git will either check out a detached HEAD or it will automatically create a new local branch with the same name as the remote branch, e.g. branch1. This new local branch will never be deleted unless to tell Git to delete it explicitly.

What is happening is twofold:

  1. You need to make sure Git is pruning the remote tracking branches (remotes/origin/branch1, etc.). By default it does not. See this question on pruning remote tracking branches for how to do this. Basically it's git fetch -p origin or git remote prune origin.

  2. When you checked out a branch to work on it, you created a local branch with the same name as the remote tracking branch. The local branch branch1 will never be deleted unless you do so manually, even if the remote tracking branch remotes/origin/branch1 is pruned, which is done if branch1 was deleted on the remote server.

You can clean up these branches manually with git branch -d (or force it with -D, which may be necessary if the branch was never merged), or look at answers to Remove old remote branches from Git for an automated way to do it.

Upvotes: 6

axiac
axiac

Reputation: 72226

Open a console in the project's directory and run:

git remote prune origin

It removes any local references to remote branches that do not exist any more on the remote repository named origin.

Replace origin in the command above with the name of your remote repository (as it is known by the local repository) in case it is not origin.

As far as I know there is no way to accomplish this goal using the IntelliJ Git plugin.

Read more at: https://git-scm.com/docs/git-remote

Upvotes: 0

Related Questions