gitgithubsquash

Coworker deleted code and then squashed; how to recover on GitHub


Sequence of events:

So the code we need isn't visible on GitHub, because it was added and then deleted in the same squashed commit. I don't have a local copy of the code, because I deleted my git directory. I just want to know how to see what's inside the squashed commit. Some people have told me this isn't possible, but that doesn't make any sense; the whole point of git is to not lose data, and the whole point of GitHub is to keep a copy even if it's deleted locally. I pushed the code to GitHub so I'm sure it's still there. I just need help figuring out how to find it my git folder or whatever.

Thanks!


Solution

  • note: fortunately, github also holds some way to trace the history of actions, but deleting your local git repository is not a very smart thing to do: some data (like your local reflog, which is veeery precious in case of disaster recovery) is not shared and only saved in your local copy.

    If you are stuck in the middle of a merge/rebase/cherry-pick, you can "return to the way it was before" by running git merge --abort/git rebase --abort/git cherry-pick --abort (if you are not sure which one should be run, try them in turn).


    Github has a so-called Events api, where you can find a trace of what was pushed in the past to your central repo: see the doc at

    https://docs.github.com/en/rest/activity/events?apiVersion=2022-11-28#list-repository-events

    You may also have your colleagues check their own local reflog (git reflog, git reflog origin/main # or origin/master or git reflog origin/some-relevant-branch). If they worked with your feature they will have a copy of your commit locally.