Monday, 30 June 2025

Revert a Specific File Committed that Pushed to Remote Git

You want to undo changes to just one file, even though the commit has already been pushed.

Option 1: Revert the file to the previous version and commit again (Safe & Recommended)

-----------------------------------------------------------------------

1. Revert the file to the version from the previous commit:

git checkout HEAD^ -- path/to/your/file

2. Stage the reverted file:

git add path/to/your/file

3. Commit the change:

git commit -m "Revert file to previous version"

4. Push to remote:

git push

Tip: You can use a specific commit hash instead of HEAD^

Option 2: Reset file to a specific older commit version (Safe if targeted)

------------------------------------------------------------

1. View commit history for the file:

git log path/to/file

2. Reset the file to that commit's version:

git checkout <commit-hash> -- path/to/file

3. Commit and push:

git add path/to/file

git commit -m "Revert file to specific commit version"

git pushOption 3: Revert the whole commit (only if the commit changed only this file)

-----------------------------------------------------------

git revert <commit-hash>

git push

Avoid:

------

Avoid using 'git reset --hard' and 'git push -f' on shared branches.

Always use a clean commit to revert individual file changes on shared remotes.