1
votes

My home computer is behind a corporate VPN firewall and is not accessible from outside.

I am trying to keep my github repo as identical as possible to my desktop's repo.

I have the following hooks:

.git/hooks/post-commit: git push origin *:*

Now I realize that if I checkout a branch, commit, and merge that branch into master, I will have a "dangling" remote branch because the commit would have created that branch.

I tried googling for a "post-delete-branch" hook to call git push origin :$(branch) but git does not provide this hook.

What can I do to remove dangling branches?

1
the --mirror switch to push is probably what you want.Nevik Rehnel

1 Answers

1
votes

You could add to your current post-commit hook a script cleaning up local branches and pushing those deletions (like in this blog post):

git branch —merged master | grep -v ‘master$’ | xargs git branch -d

echo “The following remote branches are fully merged into master and will be removed:” git branch -r —merged master | sed ’s/ *origin\///‘ | grep -v ‘master$’

read -p “Continue (y/n)? ” 
if [ “$REPLY” == “y” ] then 
    # Remove remote fully merged branches 
    git branch -r —merged master | sed ’s/ *origin\///‘ | grep -v ‘master$’ | xargs -I% git push origin :% 
    echo “Done!” say “Obsolete branches are removed” 
fi

That way, at the next commit, any branch previously merge will be deleted both locally and on the remote side.
See "How to delete a Git branch both locally and remotely?".


Don't forget at home to do a:

git fetch --prune 

That will clean up local branches which are no longer present on the remote side.
See "git remote branch deleted but still appears in 'branch -a'"