Hard reset of a single file

751    Asked by abdi_8724 in Devops , Asked on Nov 3, 2025

What commands allow us to discard local changes and restore a specific file back to its last committed state without affecting the rest of the project?

Answered by Adam Lipscomb

Sometimes while working with Git, you may want to undo changes only for a specific file without resetting your entire project. This is where performing a hard reset on a single file becomes useful. Git provides a couple of simple commands to restore that file to its last committed version.

 Hard reset a single modified file to the latest commit:

  git checkout -- filename

  • Discards any local changes permanently
  • Restores the file from the latest commit in the current branch

 If the file has been staged already:

git reset HEAD filename
git checkout -- filename

  • First command un-stages the file
  • Second command resets it to the committed state

 Reset file from a specific commit or branch:

  git checkout commit_hash -- filename

  • Useful if you want the version from an older commit

or

  git restore filename

(Recommended in newer Git versions)

 Why perform a hard reset on a single file?

  • Fix accidental edits without touching the whole codebase
  • Recover a file quickly if it becomes corrupted or messy
  • Helps maintain clean and controlled version history

 Warning: A hard reset will permanently remove your uncommitted changes for that file. Make sure you don’t need those edits before running the command.

In summary, Git makes it easy to reset just one file using commands like git checkout -- file or the modern git restore. This keeps the rest of your project safe while restoring only what you need.



Your Answer

Answer (1)

I've encountered this exact situation countless times during development projects. There's nothing more frustrating than accidentally modifying a critical configuration file and needing to quickly restore it without losing other important changes. The git checkout command has been a lifesaver for me, especially when working on complex features where only specific files need reverting. I particularly appreciate how Git allows such granular control over file restoration. During stressful debugging sessions, I sometimes take short breaks playing tiny fishing to clear my mind before tackling the code again.



6 Months

Interviews

Parent Categories