10

I created a fork of a git repo on BitBucket (let's call it fork_origin). Now the upstream repository (let's call it upstream_origin) has had numerous branches merged into it's master and deleted. So running

git fetch --prune upstream_origin

deleted lots of remote/upstream_origin/ branches, but now those same branches still exist in the remote/fork_origin/ namespace.

Is there a standard git command to deal with this? I'd like to stay away from complex bash scripts that do mass deletes on the remote repos.

UPDATE:

As suggested, I tried to use the remote prune command:

git remote prune fork_origin

However, it had no effect. Upon further investigation, that seems to work only for 'stale' branches, but when I run:

git remote show fork_origin

it shows that all of the branches are still 'tracked'. So it makes sense that the git remote prune command had nothing stale to delete. Is there a way to force the remote repo (fork_origin) to update it's branch statuses relative to upstream_origin?

Peter Groves
  • 411
  • 6
  • 14

3 Answers3

3

If you want to remove the branches on the remote use this command:

git remote prune fork_origin

But before that, take a look at this thread and see what can go wrong: git remote prune – didn't show as many pruned branches as I expected

Community
  • 1
  • 1
Daniel Gomes
  • 599
  • 6
  • 10
  • I added an update to the question regarding what this command did. Also, these are branches that have no local branch tracking them, so I don't think the issues from the other thread apply to my situation. – Peter Groves May 10 '13 at 18:27
1

For Linux and Mac users this command would delete all branches from my work that have been deleted in upstream.

git branch -a | grep origin | awk -F \/ '{print $NF}' > _my_branches.txt; git branch -a | grep upstream | awk -F \/ '{print $NF}' > _upstream_branches.txt; for deleted_branch_name in `comm -23 _my_branches.txt _upstream_branches.txt`; do git push origin :$deleted_branch_name; done

If you prefer to have steps instead of one long command:

# Find the branches in my fork
git branch -a | grep origin | awk -F \/ '{print $NF}' > _my_branches.txt; 
# Find the branches still in upstream
git branch -a | grep upstream | awk -F \/ '{print $NF}' > _upstream_branches.txt;
# See only the ones that are still in my fork with the comm command
# And only for those branches issue a git command to prunbe them from my fork.
for deleted_branch_name in `comm -23 _my_branches.txt _upstream_branches.txt`; do git push origin :$deleted_branch_name; done
Humble Debugger
  • 4,439
  • 11
  • 39
  • 56
0

There is not a single git command, but you would need to;

  • fetch fork_origin
  • identify any remotes/fork_origin/branch which isn't present anymore in remotes/upstream_origin (because of your git fetch --prune upstream_origin)
  • git push --delete thoseBranches

This is a bit similar to "Delete multiple remote branches in git".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250