When using git pull origin master to update code, you sometimes encounter conflicts that prevent merging. When you know the remote code is correct and you just need to overwrite the local code, I used to do this:
git fetch --all
git reset --hard origin/master
And I would get the result I wanted.
But today, when I repeated the same operation, an accident happened. A lot of useful data was deleted after this operation and couldn’t be recovered.
My Git environment was like this:
This was a system running in production that generates a lot of historical data in real-time in the same directory. However, due to the large data volume, it wasn’t added to git tracking. But the .gitignore file wasn’t properly written. And since it was a multi-person project, someone had executed git add --all at some point.
Just like that… precious historical data was deleted after an irreversible git reset --hard operation.
The data was never recovered…1
So when managing projects with git, keep these points in mind:
- Use
git reset --hardwith caution - Write a proper
.gitignorefile - Back up production data
- Don’t add untracked files carelessly
Notes added on 2018-10-10
This was the biggest production incident I’ve caused due to my operational mistake - losing operational historical data. I still take this as a warning to this day. ↩︎