0

I am new to git/github and have started using it recently.

I have a project in git that has a few commits, however , I forced a push into git using git push -f origin master and now, i have lost all previous commits and only have one that was pushed recently.

How do i retrieve restore my project to previous state in git?

Things that I have done: git reflog (This doesn't give me previous commit details)

213bcde HEAD@{0}: revert: Revert "V3 files" cb24c10 HEAD@{1}: reset: moving to HEAD cb24c10 HEAD@{2}: reset: moving to cb24c103e6c0400760a20098a2baceb0601bb858 cb24c10 HEAD@{3}: checkout: moving from master to master cb24c10 HEAD@{4}: commit (initial): V3> files

I need to get to a commit beyond cb24c10 head@{4}: commit (initial): V3 files

git revert HEAD --no-edit

git show This shows details only of the current commit SHA

git fsck --no-reflogs

This provides me a dangling blob and I am not sure if this is a reference to my old commit

Checking object directories: 100% (256/256), done. dangling blob e6a2438a4750e9cda68ce2d916c1b8a65dcc5384

Please can someone assist in letting me know next steps to restore my project to previous state.

Unfortunately, I haven't closed my terminal window and I can have the SHA of previous commit b0b7eba. How do I restore to this state?

git push -f origin master Delta compression using up to 4 threads. Writing objects: 100% (507/507), 13.55 MiB | 7.17 MiB/s, done. Total 507 (delta 109), reused 0 (delta 0) remote: Resolving deltas: 100% (109/109), done. To https:http://.filename.git + b0b7eba...cb24c10 master -> master (forced update)

Mahesh N
  • 772
  • 1
  • 10
  • 21

1 Answers1

1

UPDATE PER NEW INFO IN THE QUESITON

Given the original commit SHA1 of b0b7eba at least lets us try some things.

For starters,

git checkout b0b7eba

As noted below, if this is a newer clone, the command will fail. But if it doesn't fail, you're in great shape.

git branch -f master
git push -f

If it fails (and if you don't have an older repo to try with), we're again back to github; but at least we know what to tell github we want. Again I have to plead ignorance about the specific steps to use (since I don't really do much github), but you should be able to navigate to the specific commit (which almost surely will still exist there) and then either...

  • tag it, then pull, then checkout the tag, then branch -f master, then push -f
  • create a branch on it, ...
  • just move the master branch to it via the web API

ORIGINAL REPLY

So one thing to know: if you don't have a local repo that was created before the force push, this isn't going to be easy.

You mention trying reflogs, but it sounds like you're saying they don't go back far enough. Now maybe that's because the older reflogs have expired, but unless you've configured things in an unusual way I doubt it. So I wonder if you've been trying to fix this using a clone that was created after the force push; because the thing is, the reflog is local to only a single clone of the repo. So any clone created after the force push can't possibly have a useful reflog entry. If you have the clone that was used for the force push, that's the best thing to use.

Similarly, you mention finding a single dangling blob. A blob is content for an individual file; the object type you want to find is COMMIT. The fact that no dangling commits are shown again suggests that the clone was created after the force push (because at that point the commits you want were unreachable and would not be included in the pack used to initialize new clones).

(Still, if you're curious about the object you can do

git cat-file -p e6a2438a4

and see what it is.)

A force push is a history rewrite, and revert can't help with that. (It only undoes changes within a commit as understood in current history.)

Your best bet is to go into the repo from which the force push was done and check its reflog. If that's a no-go, you're at the mercy of github's API (which I don't know as much about); it seems there might be useful advice here: https://objectpartners.com/2014/02/11/recovering-a-commit-from-githubs-reflog/

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • The git checkout resulted in error (error: pathspec 'b0b7eba' did not match any file(s) known to git.), however I do have a copy/backup of the files that were pushed to git in my local (Backed up copy). Are you asking me to get inside that folder and set it as git repository and follow your steps again? – Mahesh N May 05 '17 at 18:49
  • Ok, so it seems the local does not contain the target commit and will be of no use. But no, I am not suggesting you set up a new repo to try again. A backup of the work tree files is no help. You need an old copy of the part of the repo that's in the `.git` folder - i.e. *a clone that existed before the force push*. That is the only kind of local repo that will work. So from what you're saying, I think you need to figure out how to recover the history directly through the github web interface – Mark Adelsberger May 05 '17 at 19:00
  • The git api's helped. It was just a simple get and post that helped.I got the SHa of the commit that I needed and used post method of the api to create that as a separate branch.. – Mahesh N May 05 '17 at 20:37