2

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?

  • Did you pull to your local machine after you created the readme? – mnestorov Jun 04 '20 at 11:30
  • 1
    From your script it looks like you `git push -f` to your github repo. If you have not pulled your readme file before you did such a push, then I think you'll loose the readme. Someone correct me if I am wrong. – mnestorov Jun 04 '20 at 11:33
  • No, I didn't pull. I just ran the script. Should I add a pull to the script to grab the online changes in the future before committing local changes? – Sean Straus Jun 04 '20 at 12:26
  • That might be one way to not get into such a situation. You can refer to my answer for more details. – mnestorov Jun 04 '20 at 12:29

1 Answers1

1

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.

mnestorov
  • 4,116
  • 2
  • 14
  • 24
  • Thank you *so* much for this reply! This looks very promising. I did find the event ``` "commits": [ { "sha": "8d86cfdacfd3544de902782a14b67e1681a1a991", "author": { "email": "scstraus@yahoo.com", "name": "Sean Straus" }, "message": "Update README.md", "distinct": true, "url": "https://api.github.com/repos/scstraus/home-assistant-config/commits/8d86cfdacfd3544de902782a14b67e1681a1a991" } ``` – Sean Straus Jun 04 '20 at 12:50
  • But when I do curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"ref":"refs/heads/D-commit", "sha":"8d86cfdacfd3544de902782a14b67e1681a1a991"}' https://api.github.com/repos/scstraus/home-assistant-config/git/refs – Sean Straus Jun 04 '20 at 12:52
  • I get HTTP/1.1 404 Not Found Date: Thu, 04 Jun 2020 12:45:25 GMT Content-Type: application/json; charset=utf-8 Content-Length: 116 Server: GitHub.com Status: 404 Not Found { "message": "Not Found", "documentation_url": "https://developer.github.com/v3/git/refs/#create-a-reference" } – Sean Straus Jun 04 '20 at 12:52
  • Oh my god, I found it!! I took this URL that it gave me and plugged it into my browser "url": "https://api.github.com/repos/scstraus/home-assistant-config/commits/3556812a7339ea18ebf90b11bb32f60d25ca6e95" – Sean Straus Jun 04 '20 at 12:59
  • And then in that JSON result under "file", there was the key value pair which had the raw file which I could copy into my repo. raw_url "https://github.com/scstraus/home-assistant-config/raw/8d86cfdacfd3544de902782a14b67e1681a1a991/README.md" – Sean Straus Jun 04 '20 at 13:01
  • As a side note, I have changed `git push -f` to `git push --force-with-lease`. I am also adding `git fetch origin` and `git merge origin/master` to the script to allow me to edit the README.md online while I edit the rest locally.. Hopefully that will not be a problem..? What do you think? – Sean Straus Jun 04 '20 at 13:21
  • Is there a reason why you are `fetch`ing and then `merge`ing instead of a normal `pull` – mnestorov Jun 04 '20 at 13:24
  • I just watched some videos on it and that looks safer than pull, I was afraid that pull would overwrite my changes on my local copy. I want to join the changes from the server onto my local copy and then commit and push everything together. – Sean Straus Jun 04 '20 at 15:06
  • This is my new script. It seems to work how I want it to.. Any downsides? `#!/bin/bash cd /usr/share/hassio/homeassistant git fetch origin git merge origin/master git add . git status echo -n "Enter the Description for the Change: " [Minor Update] read CHANGE_MSG git commit -m "${CHANGE_MSG}" git push --force-with-lease origin master exit` – Sean Straus Jun 04 '20 at 15:07
  • Looks okay and safer now. There is always the option to have `git pull --rebase`, but you can look into it :) – mnestorov Jun 04 '20 at 16:33
  • 1
    I will. I still have a lot to learn about git, but this experience definitely taught me a lot. Thanks to you it wasn't just learning how stupid I had been being the hard way ;-). – Sean Straus Jun 05 '20 at 12:42