11 November 2026 · One command a day
How do I throw away changes to one file in git?
git checkout -- file throws away the uncommitted changes you have made to one file. It puts the file back the way it was at your last commit or git add. You use it when an edit on the server has gone wrong and you want a clean copy back without touching anything else in the repository.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
git checkout -- config/app.conf
git checkout copies files out of the repository into your working folder. The -- tells git that everything after it is a file path and not a branch name, so it cannot mix up a file called main with the branch called main. config/app.conf is the file to restore, written relative to the folder you are in. You can list several files, or give a folder such as config/ to restore everything inside it. Run it inside the repository as the user who owns it. It does not need sudo.
What you will see
$ git status --short
M config/app.conf
M public/index.html
$ git checkout -- config/app.conf
Updated 1 path from the index
$ git status --short
M public/index.html
Before the command, both files show M, which means modified. Git says it updated one path from the index, which is its name for the staging area. Afterwards only public/index.html is listed as changed. The config file now matches the last version git had, and your other edits are still there. If you want a fuller picture before you start, there is a guide on seeing what has changed in a git repository before you commit.
When to use it
- You edited an nginx or app config on the server, the site stopped working, and you want the known-good version back straight away.
- A build step or script rewrote a tracked file, such as a lock file, and you do not want that change committed.
- You tried a quick fix on a live site and want to undo it before you pull the proper update.
Watch out for
This cannot be undone. The changes you throw away were never committed, so git has no copy of them. They do not go to a bin or a stash. Check what you are about to lose with git diff config/app.conf first (see how to see exactly which lines changed with git diff). If you might want the edit later, run git stash push config/app.conf instead. That sets the change aside, and git stash pop brings it back.
Questions people ask
Does it undo changes I have already staged with git add?
No. It restores the file from the staging area, so the staged version stays. To go right back to the last commit, run git checkout HEAD -- config/app.conf, which resets both the staged copy and the working copy.
What is the difference between git checkout -- and git restore?
git restore config/app.conf does the same job. It was added in Git 2.23 to keep restoring files separate from switching branches. Both work on Ubuntu 24.04, and many older guides still use git checkout.
Fixing a live config in place and rolling it back in seconds is the kind of thing you can only do on a server you control. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.
Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.
Affiliate link.