desktop icon indicating copy to clipboard operation
desktop copied to clipboard

Merged branches stay around until they're manually deleted

Open joshaber opened this issue 9 years ago • 48 comments

@shiftkey writes

In previous versions of Desktop, we had this behaviour for pruning:

  • after a sync was completed, this is a chance to find branches that have been merged and removed locally
  • if the current branch is in a detached HEAD or unknown state, skip pruning
  • we look for branches which have been merged into the default branch (not the current branch)
  • on Windows we also had some additional guards to skip reserved branch names, like gh-pages

I've created a branch prune-merged-braches-demo that shows how you might add this into our process, but is deliberately incomplete - the TODO statements need to be addressed as part of implementing this.

There might be some changes with how we do this, but this is a great chance for someone who cares about this feature to have a go at implementing it!

Problem statement:

Branches stick around in Desktop indefinitely until manually deleted. I want the branches that show up in Desktop to be relevant to me and not muddied by stale branches.

Proposed solution:

If

  • a branch is deleted on GitHub.com and
  • merged locally into the default branch in Desktop and
  • has no commits ahead of the default branch

then

  • they should be deleted in Desktop after a certain amount of time (to allow for people who frequently reuse the same branch for their work if that's a common workflow).

Considerations:

  1. If there's a difference between the commits of the remote on GitHub.com and your local branch, we should not delete the local branch.
  2. If GitHub has a concept of reserved branch names as @shiftkey references above (like gh-pages), we should skip pruning those branches.
  3. If there's work that's not merged into default on GitHub.com (for example, a PR that's closed and deleted), we should not delete the local branch because it has work that's ahead of the default branch.

Questions:

  1. How long before we automatically prune branches? (14 days)
  2. How frequently do we check branches for ability to be pruned? (once per day)
  3. Do we show anything in the UI to indicate that a branch will be pruned? (We don't think so - we're being intentionally conservative here and we don't think we'll put you in a position to lose any work here)
  4. Are there reserved branches here that we should account for? (unknown)
  5. Does it make sense to consider squashed and merged branches here (#1771)? (unknown)

joshaber avatar Dec 14 '16 14:12 joshaber

Copying my comment from https://github.com/desktop/desktop/issues/1128#issuecomment-302165993 at @shiftkey request.

when I choose to update all remotes it'd be nice if it run in parallel -- git remote update --prune does it sequentially.

alrz avatar May 18 '17 11:05 alrz

After I merge PRs on GitHub, their locally checked out branches end up stale in my local repo. So I often run locally the following command:

git branch --merged | grep -v "master" | grep -v "*"| xargs git branch -d

I'm thinking about opening a PR that adds a "Prune merged branches" (needs better wording) to the Repository or Branch menus.

Is this what this issue is about? Or is it about pruning remote branches that were fetched locally?

caiofbpa avatar Feb 01 '18 12:02 caiofbpa

@caiofbpa this is about pruning local branches. Your original thinking is correct.

iAmWillShepherd avatar Feb 01 '18 18:02 iAmWillShepherd

Ugh this issue is over a year old, too bad. I thought more people would be in need of something like this.

jpike88 avatar Sep 12 '18 12:09 jpike88

@jpike88 I've updated the issue with extra details about the feature and what's required, to make clear that this is something an external contributor can pick up and have a go at!

shiftkey avatar Sep 12 '18 18:09 shiftkey

@billygriffin could you clarify which branches we should be pruning. Specifically, should I

  1. Prune all branches in all repos that Desktop tracks or
  2. Only prune the branches of the currently opened repo?

If we go with option 1, there's a chance the app could be closed mid-way through the cleanup, meaning we need to persist this state to some longer term place than memory. Do you have a preferred way of handling this? Do we resume when app opens or when the scheduled time arrives in situations like this?

If we go with option 2, that may lead to a scenario where the branches are never marked for cleanup because the user briefly opened the repo before moving on, not giving us enough time to kick off our cleanup process.

iAmWillShepherd avatar Dec 05 '18 16:12 iAmWillShepherd

Good question @iAmWillShepherd.

If we go with option 1, there's a chance the app could be closed mid-way through the cleanup, meaning we need to persist this state to some longer term place than memory.

If the app is closed mid-way through the cleanup, then the next time we check, wouldn't it just do the check again and anything that didn't get caught the first time would now get pruned appropriately? I'm not certain why we'd need to persist state. It doesn't really matter if it doesn't get all the way cleaned up if we'll be checking again at some interval, right?

billygriffin avatar Dec 05 '18 20:12 billygriffin

I think we want option 1 but with some stipulations, as @iAmWillShepherd and @billygriffin mentioned above: that prune when the app when it starts again if it wasn't completed.

If we do that and we prune at X time each day (for this example 6pm), it sounds like we'd have two actions/checkpoints

  1. at 6pm we prune and, upon completion, we store when we’ve pruned**
  2. at startup, we check if we pruned at 6pm the day before. if not, we prune now***

** we can store when we have completed pruning or when we start to prune. it’ll affect very few people but it depends on whether we want to re-prune again on startup if they quit at 6:00:05pm vs. just waiting until the next day *** it can be now or like in 15m so the system can start up without also adding a spike in memory usage to prune (if that's something we're worried about)

I think the biggest risk is performance. We're going to have to see how much of a performance hit we take during pruning, though. If it's too intense, we might have to change to option 2. Maybe there's a quick way to assess that before we finalize the feature?

nerdneha avatar Dec 05 '18 22:12 nerdneha

This should take into account the last time the branch was checked out/created. I can imagine some use cases where it might be useful to locally create a branch with an old revision where it would be annoying if the branch was deleted.

j-f1 avatar Dec 05 '18 23:12 j-f1

@j-f1, how much time do you think should pass before we step in and auto-clean it for you?

iAmWillShepherd avatar Dec 07 '18 19:12 iAmWillShepherd

I’m not really sure. Perhaps a couple of weeks?

j-f1 avatar Dec 08 '18 02:12 j-f1

Some additional thoughts:

  1. Prune all branches in all repos that Desktop tracks or
  2. Only prune the branches of the currently opened repo?

I favour Option 2 because this is about pruning local branches - there's not much value in trying to clean up repositories you don't use regularly. And if your active repositories are pruned more often there will be fewer redundant refs to manage locally.

Also, I wouldn't worry about implementing checkpoints and restarting things in the first version of this feature if Desktop was to suddenly close between sessions - identifying which branches can be pruned is relatively straight-forward:

$ git branch --merged master

And if the app closes while working through the list, it can just start over from the next session and query again for candidates.

This should take into account the last time the branch was checked out/created.

This is an interesting idea, and the reflog is the place I'd look at for events associated with a certain ref:

$ git reflog --date=relative
92e0113ef (HEAD -> master, origin/master, origin/HEAD) HEAD@{73 minutes ago}: checkout: moving from electron-3 to master

You probably want something that's easier to parse and compare to the current time:

git reflog --date=iso
92e0113ef (HEAD -> master, origin/master, origin/HEAD) HEAD@{2018-12-18 07:56:39 -0400}: checkout: moving from electron-3 to master

shiftkey avatar Dec 18 '18 13:12 shiftkey

The changelog mentions that a merged or deleted branch will be pruned after 2 weeks. Why after 2 weeks? Why do I need to see the deleted branch in the dropdown for 2 extra weeks after it was deleted? No option to manually trigger this 'cleaning'?

IdanAdar avatar Feb 18 '19 08:02 IdanAdar

@IdanAdar Thanks for the feedback. We wanted to start conservatively on this since it’s a potentially destructive action and because it’s being newly introduced in the app - previously branches deleted on GitHub would remain forever until manually deleted in Desktop. The goal here is to make sure that the list doesn’t get so long with irrelevant branches that it becomes basically useless, and we think this is a good step toward that. We’re definitely open to making the logic more aggressive as we get feedback from folks, but we’re going to start here and see how things go and whether it’s alleviating the problem.

billygriffin avatar Feb 18 '19 21:02 billygriffin

I'm wondering if there a popup that warns you before deletion and offers you to keep certain branches?

tooomm avatar Feb 18 '19 22:02 tooomm

@tooomm There isn't. We decided in taking a very conservative posture that we would only delete branches that aren't actively in use (hence the 14 days requirement) and don't have anything on them that's ahead of the default branch on GitHub. This combination, in addition to the branch being deleted on GitHub.com, seems relatively uncontroversial, but we're definitely going to listen to feedback we receive in the weeks and months after this ships to make sure it aligns with people's expectations and isn't overly aggressive.

billygriffin avatar Feb 18 '19 23:02 billygriffin

we would only delete branches that aren't actively in use (hence the 14 days requirement) and don't have anything on them that's ahead of the default branch on GitHub.

I wasn't aware of the second part. 👍

tooomm avatar Feb 18 '19 23:02 tooomm

Hi,

Sorry about my inexperience, but just to let you know that this issue is apparently still happening to me: when I delete a branch on the web, GitHub Desktop doesn't delete it - I have to do it manually every time.

Thanks!

Kradirhamik avatar Jun 14 '19 21:06 Kradirhamik

@Kradirhamik Stay tuned for branch pruning as it will be released in the coming weeks. Thank you for the patience.

tierninho avatar Jun 14 '19 22:06 tierninho

Is there a setting to disable this behaviour?

While I see the potential advantages I still want to be in control when stuff gets deleted.

Croydon avatar Jul 02 '19 16:07 Croydon