6

I did a git push --force thinking it would only push the current branch but it did all branches. The real damage was done to only a couple of my colleague's branches that I haven't pulled for a long time so, by doing this (which was enabled by --force), I completely reset all the commits on the remote that have been made since I last pulled.

Without going to my colleagues to ask them to push their up-to-date local branches to GitHub to undo the damage I did, is there anything I can do to undo it? What I did was unfortunately equivalent to git reset --hard, meaning I nuked all the commits since. Is there a way I can undo that?

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • Ouch. I don't have an answer for that, but as a suggestion, you can change this behaviour of Git using the `push.default` setting (for example, by setting it to `current`, `upstream` or `simple`). – helmbert Apr 21 '15 at 22:30

1 Answers1

3

No. If you don’t have the old commits those remote branches were pointing to in your local repository, then you’re out of luck. GitHub does not give you this ability.

You will have to ask your colleagues to force-push their branches again to restore the previous state.

If a simple git push updated all branches, then you might want to check your push.default configuration. I recommend either simple or upstream to avoid mistakes.

And this hopefully tells you to avoid force-pushing anything in the future ;) It’s dangerous, and now you have experienced why. If you want a little more security, you can use git push --force-with-lease in the future. That way, Git will not force push anything if your remote branches does not match the actual remote’s state. So if you haven’t fetched in a while, you cannot force-push. It won’t prevent you from making all mistakes, but maybe a few. And in this case, it would have allowed you to restore the original state (since it essentially requires that you have the remote’s state in your repository, so you can go back).

poke
  • 369,085
  • 72
  • 557
  • 602
  • 10
    actually, i was able to do it: `git reset --hard origin/remotebranch@{1}` – amphibient Apr 21 '15 at 22:37
  • 2
    @amphibient But that should only allow you to revert to your old version of that remote branch. If you haven’t fetched recently, then that version is still outdated. – poke Apr 21 '15 at 22:40
  • 1
    it is resetting off of the remote, not local. – amphibient Apr 21 '15 at 22:48
  • This also worked for me. I suspect it is because of an earlier git fetch pulled them down. – Nathan Lee Jan 20 '17 at 06:48
  • 1
    This is incorrect. GitHub will still have the hash of the old version and you can get it from GitHub, **even if you don't have the hash in a local reflog**. See the question this is marked as a duplicate of. – mikemaccana Nov 17 '20 at 17:26
  • 1
    @amphibient you just really saved my ass/sanity thanks so much :) :) – S.. Mar 17 '22 at 13:33