Recover changes to README.md made online on github site that were lost when I made a commit from my local repo

Viewed 916

So, I had a repo that I had always updated only from my local machine by using the following script.

#!/bin/bash

cd /usr/share/hassio/homeassistant
source /srv/homeassistant/bin/activate
hass --script check_config

git add .
git status
echo -n "Enter the Description for the Change: " [Minor Update]
read CHANGE_MSG
git commit -m "${CHANGE_MSG}"
git push -f origin master

exit

In between running that script, I decided to finally create a long and detailed README.md for my repo using the online editor like in this screenshot where I just edited the file and only pressed the green button to commit the change.

Photo of how I did commit from github online editor. I just edited and pressed the green button many times.

However, when I ran the aforementioned script again the next time, the README.md is gone and there is no trace of any of those commits ever existing. If I run a git reflog I see 9e63827 which is the most recent time I ran the shell script, and 54b6047 which is the previous time I ran the shell script, but I don't see any of the 100 commits I made online to the README.md in between.

root@narnia:~/hassio/homeassistant# git reflog
9e63827 (HEAD -> master, origin/master) HEAD@{0}: commit: Finally finished moving to new iOS app on all devices, cleaned up old trackers, speedtest to automation so I can turn it off, withings integration
54b6047 HEAD@{1}: commit: Fixed Zwave Battery Sensors and Warnings, first stab at appliances tab
2a5768f HEAD@{2}: commit: Heating Python Script, Heating window automations, fixed popup lovelace

When I run git fsck --lost-found I get 3 dangling blobs

Checking object directories: 100% (256/256), done.
dangling blob d370200927d6739d8156f8e2672cd4889f558547
dangling blob f7a1c6af989d3f41117f209f9a524821691c116d
dangling blob c61265cf4e353d0a474e29b33ed066be632164fd

None of which contain any trace of my README.md when I check them with a command like git show f7a1c6af989d3f41117f209f9a524821691c116d

I can't believe all those hours of work on my README are really gone are they? What the heck happened and how do I get them back?

1 Answers

From what you are saying, it looks like you did not pull the changes that you've done when editing the Readme file through GitHub.

Since you created commits, which create the Readme, through the GitHub page, those commits are found only on the remote repo. You don't have them yet locally. That's why you need to pull after you create something through the GitHub UI.

Now, judging from your script and that last line

git push -f origin master

It doesn't seem that you pull or take the remote changes anywhere. Thus what happened, most probably, is that you created a local commit, that commit does not introduce a Readme file. Actually, your whole local repo, as far as we can tell, does not have that Readme. And by force pushing, you just overwrite your remote to look like your local repo. This would result in lost changes.

What can you do? I think you should try this answer here. I don't know if it will work, but it's worth a shot.

Also, for the future, you can replace the git push -f with git push --force-with-lease (allow refs that are not ancestors to be updated if current ref matches expected value), or in plain english, you can force push only if the upstream branch is not updated.

Edit You can also add a git pull step as well.

Related