简体   繁体   中英

Git detect squash and merged branches

I want to know if there is a way to detect if a squashed (on remote after approving a pull request) branch is merged into develop. Usually, I use

git branch --merged

However, this seems not to work due to squashing.

The short answer is that there's no general yet reliable way to tell.

In many specific cases, it's easy to tell by comparing the contents of the new commit added to the upstream repository to the contents of the merge you proposed. That is, in your repository—and/or on the clone you control on GitHub, if you and they did the merge through GitHub—you have:

...--G--H   <-- origin/somebranch, upstream/somebranch
         \
          I--J--K   <-- feature/yours

They used "squash and merge" to combine your three commits I , J , and K into one big IJK commit, which they added after commit H , so that their repository now reads:

...--G--H--IJK   <-- somebranch

The contents—as in, the associated snapshot —of commit IJK exactly match the contents (snapshot) of your commit K , and the log message of commit IJK is something that tells you that they squash-merged feature/yours . So you now know that it is safe to resynchronize with them and delete feature/yours , using IJK instead.

However, if they added IJK after some other commit L , they now have:

...--G--H--L--IJK   <-- somebranch

In this case, the snapshot for commit IJK matches what you would get if you cherry-picked their L , or merged your feature/yours with their upstream/somebranch after fetching. Discovering this is more difficult. It could be automated for many cases, but there's no clicky button on GitHub to do so, for instance. Furthermore, such automation would fail if they had to modify your work to make their IJK —though luckily (?) for you most upstreams just won't do that, forcing you to adapt your IJK sequence yourself by rebasing or merging anyway. :-) In that case we are back to the earlier situation, where your adapted branch tip commit matches the one they make with their squash-merge operation.

As Joe Phillips says in a comment , the whole practice of squashing like this is somewhat dubious. I would not go so far as to say it should never be used, but I would say that it should be rare rather than the norm in most situations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM