Search code examples
gitgithubversion-controlgit-commitgit-push

How to move back to an old version of git repository after having lost all previous commits?


Backstory :

My repo (say 'ABC') is hosted in github (but I don't have a version of it locally).

I wanted to add few more files to the same repo.

What I did :

I created a new local directory (say 'xyz')

Moved into it and initialized git

cd xyz
git init

added git remote to link 'ABC'

git remote add origin {link to ABC}

created some new files and committed them

git add ./
git commit -m "commit message"

Next, when I tried pushing the new commit to github

git push origin {branch-name}

I received a warning that some files of the remote repository are missing in my local machine.

Then, I tried a forced push

git push -f origin {branch-name}

Naturally (which I didn't knew then) I lost all my previous files of 'ABC' and all I have now is 1 single commit (the forced push one).

Also; I don't even have the SHA's of previous commits (atleast I couldn't find them) so, git checkout <commit> doesn't works. Nor does git reset or git revert

But searching around in the ABC github repository, I find a tree which shows exactly the files I want to get back. Can I use this tree anyhow or any other steps, to get back my lost files.

Please ignore any mistake and my childlike writing. Some of the technical descriptions may-be wrong as I'm new to git. Feel Free to ask back.

Thanks in advance!

**

Present State of my ABC repo with 1 commit : https://github.com/rdrsadhu/UEM-Kolkata-B.Tech

[this is not true anymore. I restored my files by commit c005a51 ]

The tree of ABC repo I'm talking about : https://github.com/rdrsadhu/UEM-Kolkata-B.Tech/tree/9e394c24e708e590ec72e5d772be13749fb8cad3


Solution

  • Go here:

    https://github.com/rdrsadhu/UEM-Kolkata-B.Tech/commits/9e394c24e708e590ec72e5d772be13749fb8cad3
    

    In the Tree drop down create a new branch on this commit (may work also in your tree link). Now the commits should be restored under that branch. Fetch the branch to your local, and reset it to master. Then push force.