Home > Back-end >  Git Pull wiped out everything on my local directory
Git Pull wiped out everything on my local directory

Time:10-10

I am new to git and GitHub. Earlier today I pushed some of my files which included .py files and some images. After I pushed it I saw that there were 2 different branches main and master. I wanted to have just one branch so I googled how to push code to the main branch without having the 2nd branch I am looking for(master). Before doing this I deleted the master branch on GitHub which had all the files and then I set out to push my code files to the main branch. From one of the google results I found, I performed git pull. Now all I have on my local repository and the GitHub repository are 3 files (readMe,gitignore and LICENSE). I want those code files back as I do not have any local backup as well. Can some please help?

CodePudding user response:

You can recover deleted branches and commits using git reflog. It will give you recent commits and their IDs, just append it to git checkout to checkout that specific commit, and checkout your branch back when you are done recovering your files.

Otherwise look into resettings your current branch onto that commit with something like appending the commit ID to git reset --hard.

CodePudding user response:

You’re in luck as deleting branches doesn’t really mean all is lost.

When you created/pushed to the master branch, a new HEAD was created which pointed to the latest commit.

Even though you’ve deleted the branch, the HEAD reference to the commits still exist.

This gives you a way to recover the deleted branch using git reflog.

Checkout the main branch & run git reflog.

You should get something like:

hfjd76g HEAD@{0}: checkout: moving from master to main
6475837 HEAD@{1}: commit: YOUR DELETED COMMIT
849hgu6 HEAD@{1}: checkout: moving from main to master
849hgu6 HEAD@{2}: random commit on main

Considering 6475837 is the commit you want to restore, run:

git checkout -b recovered_master 6475837

This will restore master as recovered_master, which you can then merge etc.

  • Related