15

I accidentally did a push --force on the wrong repo (too many termminals open), effectively resetting the master branch back to an earlier commit.

Looking at my build system I can see that the commit used to point to XYZ, however I don't have that commit locally as I hadn't done a pull or fetch recently. The repo is in Github, and I can navigate in Github to view the commit, so I know it is there.

How can I pull down the commit so I can reset master back to the right commit without having to bother the dev who pushed that change?

checketts
  • 14,167
  • 10
  • 53
  • 82

3 Answers3

39

You can create a branch from an orphaned commit in the Github GUI by doing the following:

  1. Browse to https://github.com/yourOrg/yourRepo/tree/commitHash
  2. Click on the branch dropdown
  3. Type in the new branch name and hit Enter

Now that you have a branch, you can use your Git client to check it out as normal.

Github create branch drop down

Notes

This post was helpful as I researched this. It basically states that you are stuck unless you have a local repository containing the orphaned commits. My approach allows you to add a branch so the commit is no longer orphaned.

Community
  • 1
  • 1
westse
  • 615
  • 5
  • 8
  • 2
    I wrote [a script](https://github.com/cryslith/git-recover-commit) which does the same thing using the pure Git protocol, without using GitHub's UI. – Lily Chung Nov 07 '19 at 07:03
  • For Gitlab, a similar method works: if you can find the commit page, ex: https://gitlab.kitware.com/account/project/commit/e431ddca then click `Option` > `Tag` and choose a tag name. Then you can `git fetch --tags` and `git checkout chosen-tag-name`. – Gabriel Devillers Dec 02 '19 at 08:54
  • @LilyChung You should definitely post you script as an answer to give it more visibility. You saved my day! – nstCactus Sep 30 '20 at 16:41
0

Is XYZ is reachable by some branch you can simply fetch. If not, please follow @weste's instructions to get it pointed to by a branch and then fetch:

git fetch
git checkout master
git reset --hard XYZ
git push -f origin master
Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
0

I understand currently you don't have the commit locally; nevertheless you can get it, without modifying your workspace, simply like this:

git fetch

After that, if ever you have current modification, or something like that, you can stash them temporary:

git stash save "My work to be completed later"

Then you can move back to your master branch:

git checkout -f master

Then, force to move the HEAD to the orphan commit you want:

git reset --hard XYZ

As alternative, in case, you prefer destroying and recreating your master branch, you can "move" to another branch, destroy the master branch and recreate it:

git checkout -b anyOtherBranch
git branch -D master
git checkout -b master XYZ

And eventually, push force your new master branch;

git push --force origin master
Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
  • Thank you for your answer, Bsquare! When I made the bounty I intended to directly award westse's answer which helped me greatly in a tight spot. Unfortunately StackExchange's policy forces me to wait for 24 hours, which is probably why the question showed up for you. – Jimmy He Oct 24 '18 at 00:38