There is no built-in command in Git for this task. You have to write a script in your favorite language. I'll use Powershell but you can always transform it to some fancy *SH one-liner.
Test rig
Script: gist.github.com/tkirill/7c14331de4da814069af. It does:
- Creates a repo
test-repo
with two branches: branch1 and branch2.
- Clones the repo to
test-clone
, and goes to this clone and checkouts all branches. The last step effectively creates corresponding local branches.
- Removes branches
branch1
and branch2
on origin.
- Leaves you in
test-clone
.
Removing branches
Script: gist.github.com/tkirill/0f0f1902beb0c0723582. It does:
- Gets local branches.
- Gets remote branches without remote name.
- Removes every local branch that not exists on remote.
Running experiment
.\test-rig.ps1
..\remove-local-branches.ps1
Output of the last command shows us that both branch1
and branch2
were deleted:
Removing branch branch1
Deleted branch branch1 (was ab1575a).
Removing branch branch2
Deleted branch branch2 (was c9a24c2).
But that's not the end
Writing such script can be pretty tricky if you want a universal solution. For example, the script takes branches from all remotes, not only origin. You may want to work with origin only.
But the main problem from my point of view is that it can remove local branches which not merged in develop.
Remove merged branches only
I think this is really what you need. Merged branches have little use. There are a lot of scripts and aliases doing this task since, it is really common. Choose what you like the most :) As for Windows I wrote corresponding extension for posh-git.