-1

I recently joined a project where we have a master branch but also a lot of stale branches that haven't been used in years. I have to clean the repo so that ideally only the master branch is left in the end.

Now I have seen that most branches are only "XXX commits behind master" (and no commits ahead of master). Does this mean that these branches are like screenshots of the master branch at that specific point back in time? So their contents, if not explicitly deleted by the commits to the master branch, should be in the master as well, right?

For context: I am not trying to update the old branches, I am only trying to figure out if I can delete them, which is the case if their contents are included in master or have been explicitly deleted in master.

  • 2
    Yes, if they do not show commits _ahead_ it means that they did not diverge. I think you can list those branches easily with `git branch -r --merged some-remote-branch-like-master`. – eftshift0 May 02 '23 at 09:14
  • See [How to delete all remote git branches which have already been integrated?](https://stackoverflow.com/questions/8229737/how-to-delete-all-remote-git-branches-which-have-already-been-integrated) – Guildenstern May 02 '23 at 19:54

1 Answers1

0

You could use this command:

git merge-base --is-ancestor <commit sha-1> HEAD

From the man page:

       --is-ancestor
           Check if the first <commit> is an ancestor of the second <commit>, and
           exit with status 0 if true, or with status 1 if not. Errors are signaled
           by a non-zero status that is not 1.

Checkout master, use the SHA-1 hash for the tip of the branch, the command gives you 0 if the SHA-1 is included in master.

Gauthier
  • 40,309
  • 11
  • 63
  • 97