Search code examples
gitgit-commitrevert

git - How to undo changes to a locally committed file


I did following, all while on my local BranchA:

  1. Modify FileA (by mistake)
  2. git add FileA
  3. git commit -m "Modified FileA"

So, FileA is in my local commit (never pushed to remote) on my local branch BranchA (also never pushed to remote).

How do I revert changes to FileA?

UPDATE AND HOW I SOLVED IT

I undoed changes to FileA from SourceTree instead from git as I realized it is simpler for me.

To do so, in SourceTree, r-c on commited file FileA > "Log Select ..." > select the previous commit (the one before yours) > r-c on it and choose "Reset to this commit". That did the job but thank you all


Solution

  • To specifically undo the changes in that file but keep the commit unchanged otherwise :

    1) Revert fileA to its previous state

    git checkout HEAD^ -- path/to/fileA
    

    2) Include that in the last commit you did on BranchA

    git commit --amend
    

    (And since it has not been pushed yet, no need to push with force next time.)