Search code examples
gitgithubsquash

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


Sequence of events:

  • I add code adding a feature X. It works, I push it to our company's GitHub.
  • Couple of coworkers pull my code and do some refactoring to make it match the API with existing feature Y. As part of this, they delete significant parts of the code for X that was basically duplicated with similar code for Y.
  • They run some tests. Seems good. They squash their commits together with mine, also rebase on main, and push to GitHub. (I was unaware of this.)
  • Couple days later, they notice that one part isn't working right: an example piece of code that worked when I pushed, is no longer working correctly. We try to debug, and can't find anything wrong. They ask me if I can help fix it.
  • I try to pull from GitHub. Because of the rebasing and squashing, GitHub gives me a bunch of errors and like 50x merge conflict hell. I just delete my git directory out of frustration and pull it fresh.
  • I start debugging and see the squashed commit. It turns out the bug was there by the time the squash was done, i.e. the bug was introduced during their refactoring. At this point we're pretty sure the culprit is the large chunk of "duplicate" code for X that they deleted, and we want to go back to the correctly functioning original verison, where X had its own implementation.

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.