hartwork/git-delete-merged-branches

Option to delete local branches if remote deleted (?)

mpelzsherman opened this issue · 8 comments

Is there an option to delete local branches if the remote branch has been deleted?

If not, that would be a nice addition.

Thanks for creating this nice tool!

Hi @mpelzsherman, glad the tool is of use to you!

I cannot guarantee that this feature will have a place in git-delete-merged-branches, I'd like to first understand the idea better.

You want to have the tool delete branches that have been deleted on a remote independent of being being merged or not — correct? (If they were merged than git-delete-merged-branches should already have you covered, so I'm assuming primarily unmerged ones).

With multiple remotes we would need to know which remote(s) should have that effect/power/authority and which not. Also, if the remote branch is gone by now, how do we detect locally that there was one and it was deleted? Does git remote update --prune leave any traces to draw conclusions from? How about before — did you have a specific implementation draft in mind? Lots of questions 😃

PS: It seems like git remote update --prune leaves remote tracking information intact, so it would be available for inspection. For proof:

# git --version
git version 2.43.0

# cd "$(mktemp -d)"
# git init origin
# git --git-dir origin/.git/ commit --allow-empty -m 'Dummy commit'
# git --git-dir origin/.git/ branch branch123
# git clone origin fork
# git --git-dir fork/.git/ checkout -t origin/branch123
# git --git-dir fork/.git/ config --list | grep branch123
branch.branch123.remote=origin
branch.branch123.merge=refs/heads/branch123

# git --git-dir origin/.git/ branch -d branch123
Deleted branch branch123 (was 9035710).

# git --git-dir fork/.git/ remote update --prune
# git --git-dir fork/.git/ config --list | grep branch123
branch.branch123.remote=origin
branch.branch123.merge=refs/heads/branch123

Hi @hartwork ! Thanks so much for your quick response!

Yes, I want GDMB to delete any branches that no longer exist in the remote repo, regardless of whether they were merged or not.

My team is using gitlab. If I use git ls-remote origin <branch>, and it returns nothing, running GDMB should flag the branch for deletion, yes? But in some cases, it doesn't. Maybe it has something to do with how gitlab is deleting the branches, or maybe I need to run some command to clean up my repo?

If I run git config --list | grep <branch>, I do see the branch listed. So git seems to be holding onto it for some reason. Here is the output of that command:

$ git config --list | grep topic/master/reverse-buffer-ir-202
branch.topic/master/reverse-buffer-ir-202.remote=origin
branch.topic/master/reverse-buffer-ir-202.merge=refs/heads/topic/master/reverse-buffer-ir-202
(spiet-venv)

Anyway - it's only happening for a few branches - GDMB is deleting most of them. Sorry I don't have better info for you.

Thanks again!

Hi @mpelzsherman,

I have a feeling that especially with name "git-delete-merged-branches" deleting branches that we do not know the merge status is not an ideal match for this tool: A branch being gone can mean a ton of different things, and actually deleted branches should be detectable as merged once the fork's main branch has been updated via pushing.

I wrote a small Python >=3.8 Gist find_local_orphan_branches.py now. Could you review and test it to see if it solves your problem? It could then accompany git-delete-merged-branches and allow git-delete-merged-branches to stay true to its mission.

What do you think?

Hey @hartwork - thanks so much for that script! It works great.

This is your baby, so of course it's up to you, but I might fork the project so I can incorporate your script to include an option to delete "orphaned" branches as well.

FWIW, I'm pretty sure my use case is extrremely common when working in software teams. Often I pull down branches from other devs to perform functional tests or code reviews. When those branches are merged and subsequently deleted, I want a tool to remove them automatically from local repo.

Hi @mpelzsherman,

let me try to re-summarize my understanding of the situation, if this reads wrong please let me know:

  • when the remote branch disappears because it was merged, git-delete-merged-branches will detect the local branch as merged
  • if it does not detect them as merged, it's because one or more of the branches selected as required merge targets does not have them merged locally.
  • when the remote branch disappears because it was not merged:
    • it is not clear whether the branch has been merged (and it seems impossible to determine)
    • hence we're at risk of deleting not-merged branches (and may even make the user believe they would be merged if we're not extra careful, after all this git-delete-merged-branches…)
    • deleting not-merged branches is in conflict with the name, mission, and (arguably some) users' expectations

I have some hope that you will find an approach running two separate commands in sequence, one being git-delete-merged-branches and one being something else. Forking git-delete-merged-branches (not only as a repository but as a project, will be under "GPL v3 or later" license) to add a feature like that has the potential to split the community, and I'd consider that pretty sad, and I'm hoping that the future will be different. Have you checked for other existing and maintained tools that are available that can delete orphan local tracking branches already today without writing new code? I haven't used e.g. https://www.npmjs.com/package/git-removed-branches myself but from reading the description, it could be a match? What do you think?

Best, Sebastian

@hartwork Thank you again so much for your thoughtful and prompt reply!

As it turns out, git-REMOVED-branches does exactly what I need.

Closing this issue. You are awesome. 😄

@mpelzsherman I'm glad about your positive reply. Wish you a nice evening! 🙏