Roll back a Git commit and update safely on a VPS
Take your project back to an older commit, force push to the remote, then reset the VPS to match without a merge conflict, then rebuild the service and clear caches.
On this page
When new code breaks and you want to return to a commit that worked, the safe flow has three
steps: reset locally and force push, reset the VPS to match the remote (do NOT use git pull),
then rebuild the running service. This guide walks each step with copy-paste commands.
Replace <commit-hash> with the commit you want to return to, /opt/apps/myapp with the
project path on the VPS, and myapp with your app/service name.
On your local machine
If your code broke and you want to drop recent commits and go back to an older commit:
# 1. Reset the current branch to the old commit (discard later changes)
git reset --hard <commit-hash>
# 2. Overwrite the remote history with the current one (force push)
git push origin main --forceOn a team, force push can wipe out someone else's code. Tell the team before you do it.
On the VPS (remote)
After the force push, the VPS still has the pre-rollback code. Do NOT use git pull here:
the history has diverged and a git pull easily causes a merge conflict. The safest fix is
to reset so it matches the remote branch 100%:
# SSH into the VPS and go to the project folder
cd /opt/apps/myapp
# 1. Fetch the latest from origin (without auto-merging)
git fetch origin
# 2. Hard reset the current branch to match origin/main exactly
git reset --hard origin/mainRebuild and restart the service
Once the VPS code is back to the old version, rebuild/restart to update the running app.
Next.js, React, Node.js in general:
npm install
npm run buildPM2 (process manager):
pm2 restart myapp
# or
pm2 reload allDocker Compose:
docker compose down
docker compose up -d --build.NET applications:
dotnet publish -c Release
# Then restart the service, e.g. systemctl restart myappA note on caching
If you rolled back and restarted the service but the reloaded page still shows the old UI or feature, 99% of the time it's a cache. Check and clear these caches:
- Browser (use an incognito window, or enable Disable cache in the Network tab)
- CDN (for example Cloudflare)
- Service Worker (Application tab in Chrome)
- Next.js or other SSR frameworks (delete the build folder
.next,dist...)
Troubleshooting
| Symptom | Fix |
|---|---|
| VPS reports a merge conflict on update | Don't use git pull; run git fetch origin then git reset --hard origin/main |
| Rolled back but the page still shows the old version | Clear the browser, CDN, Service Worker and build-folder caches |
force push is rejected | Check remote permissions and the branch name; make sure it's origin main |