Amidst the information presented by git help fetch
, there is this little item:
-p, --prune
After fetching, remove any remote-tracking branches which no longer exist on the remote.
So, perhaps, git fetch -p
is what you are looking for?
EDIT: Ok, for those still debating this answer 3 years after the fact, here's a little more information on why I presented this answer...
First, the OP says they want to "remove also those local branches that were created from those remote branches [that are not any more on the remote]". This is not unambiguously possible in git
. Here's an example.
Let's say I have a repo on a central server, and it has two branches, called A
and B
. If I clone that repo to my local system, my clone will have local refs (not actual branches yet) called origin/A
and origin/B
. Now let's say I do the following:
git checkout -b A origin/A
git checkout -b Z origin/B
git checkout -b C <some hash>
The pertinent facts here are that I for some reason chose to create a branch on my local repo that has a different name than its origin, and I also have a local branch that does not (yet) exist on the origin repo.
Now let's say I remove both the A
and B
branches on the remote repo and update my local repo (git fetch
of some form), which causes my local refs origin/A
and origin/B
to disappear. Now, my local repo has three branches still, A
, Z
, and C
. None of these have a corresponding branch on the remote repo. Two of them were "created from ... remote branches", but even if I know that there used to be a branch called B
on the origin, I have no way to know that Z
was created from B
, because it was renamed in the process, probably for a good reason. So, really, without some external process recording branch origin metadata, or a human who knows the history, it is impossible to tell which of the three branches, if any, the OP is targeting for removal. Without some external information that git
does not automatically maintain for you, git fetch -p
is about as close as you can get, and any automatic method for literally attempting what the OP asked runs the risk of either deleting too many branches, or missing some that the OP would otherwise want deleted.
There are other scenarios, as well, such as if I create three separate branches off origin/A
to test three different approaches to something, and then origin/A
goes away. Now I have three branches, which obviously can't all match name-wise, but they were created from origin/A
, and so a literal interpretation of the OPs question would require removing all three. However, that may not be desirable, if you could even find a reliable way to match them...
npx git-removed-branches
(dry-run) ornpx git-removed-branches --prune
(for real). You need to already have node.js installed. See answers below for details.