gitgithub

Getting a history of which commits were force pushed


Yesterday, we had a member of our team do a git push --force inside of our Bitbucket repository. I am aware of who it was, but I am wondering if it is possible to gather which commits were force pushed into the repository.

The information I am looking for from the git log / history is something like this -

+ 1951097...9b070f0 tower -> origin/tower (forced update)

I saw this message when I was trying to pull into my local branch and encountered merge conflicts after the force push, but I havent been able to see it anywhere else except that one time. Is there a place where I can see all of the commits (specifically ones with that (forced update) tag next to them?) Running Git reflog and git log do not seem to return this information.

I saw this post(which is hilarious) that is similar - How can I find out who force pushed in git?

Instead of looking for the specific person (like the asker in the previous question), I am just looking for the commits that were pushed into the repo with --force after I clone it locally. Is this information possible to gather, or am I out of luck?

Thanks.


Solution

  • 2017: My old answer was about finding who did the forced push, not about what was force-pushed.

    If you have access to the server, you should find a trace of the old commits (before the push) with git reflog.

    From there, you can infer the new commits which replaced the old branch history.

    Note that if you do not have access to the remote server (typically: github.com), you can still get the old branch SHA1 (the one before the forced push) with the "poor man's reflog", aka the push events (GitHub Events API).
    See "Does GitHub remember commit IDs?": look for any recent push events on the right branch: the last one would be the one forced, the previous one would represent the old SHA1 commit.


    2024: agilgur5 suggests in the comments:

    I can see a good amount of events in the new Activity Feed UI, github.com/:user/:repo/activity.
    You can filter per branch and by force pushes too. Hopefully saves folks some time from having to write up API queries!