Vũ Văn HảiFull-stack · AI-native
GuidesBlog
Discuss a project

© 2026 Vu Van Hai · Written from real deployment experience.

HomeGuidesBlogRSS
  1. Guides
  2. /Dev tools
  3. /Roll back a Git commit and update safely on a VPS

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.

Updated: Sep 21, 20262 min read
GitVPSDocker
On this page
  • On your local machine
  • On the VPS (remote)
  • Rebuild and restart the service
  • A note on caching
  • Troubleshooting

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 --force

On 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/main

Rebuild 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 build

PM2 (process manager):

pm2 restart myapp
# or
pm2 reload all

Docker Compose:

docker compose down
docker compose up -d --build

.NET applications:

dotnet publish -c Release
# Then restart the service, e.g. systemctl restart myapp

A 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

SymptomFix
VPS reports a merge conflict on updateDon't use git pull; run git fetch origin then git reset --hard origin/main
Rolled back but the page still shows the old versionClear the browser, CDN, Service Worker and build-folder caches
force push is rejectedCheck remote permissions and the branch name; make sure it's origin main
NextDebug and bypass SSL pinning on an Android emulator

Related articles

  • Where to put apps on a VPS: the /opt/apps directory layout

    A simple convention for app code on a VPS: keep each app in its own folder under /opt/apps, chown the parent folder once, and git clone, git pull and .env edits never need sudo again.

    VPS

    VPS
  • Automatic maintenance page with Caddy during Docker deploys

    Use Caddy's handle_errors to catch the 502 Bad Gateway that appears while a container rebuilds and serve a static maintenance page automatically, with no manual on/off switch.

    VPS

    VPS
  • Set up Caddy as a reverse proxy on a VPS with Docker

    Run Caddy as a Dockerized reverse proxy on a fresh VPS: create a shared network, write the Caddyfile and docker-compose file, get HTTPS automatically, then add new domains with a single zero-downtime reload.

    VPS

    VPS

Written by Vu Van Hai

I'm Hai, a full-stack developer based in Ho Chi Minh City. These guides come from systems I built and run myself. Need to build or untangle something similar? Get in touch.

Discuss a projectMore guides

Spot a mistake or a command that no longer works? Let me know

On this page

  • On your local machine
  • On the VPS (remote)
  • Rebuild and restart the service
  • A note on caching
  • Troubleshooting