The custom Git tooling that our team uses creates a branch for each new feature/bug that is worked on.
Often times when the change is nearing completion, the remote branch will be altered via another tool, leaving the local copy of the branch out-of-sync.
A question that often comes up is how to clean up all of the stale branches that are left in developers local repositories. A shortcut is desired since typing out many git branch -D branchname
can be tiresome when there are dozens of these stale branches.
Some advice that I had been giving out is to delete files from their .git/refs/heads
folder.
Here is the full text that I send out:
One manual trick to quickly delete branches in batch is to delete files from your
.git/refs/heads folder
. You will see filenames that correspond to branches. A branch is just a pointer to a sha that makes up the file's content, so deleting the file will delete the branch. Note that this will only delete the pointer, not the actual commit that the pointer happened to be pointing to.
This advice seems safe, but I am paranoid that there is a situation where this approach could fail or cause work to be lost.
Other than the user making an obvious mistake such as deleting the master
file, is this safe advice?
EDIT: This question is different from this question because due to the nature of our in-house tooling, often --merged
will not be sufficient to determine if a local branch is stale or not. Also, we don't want to delete branches in one-fell-swoop...a manual check for each branch needs to take place to avoid deleting still-active branches.