John Doe John Doe - 4 months ago 23
Git Question

How to revert and fix the file changes in Git

I am using git, I added some files its showing those files in the modified list. I did git diff on that same file and found some unintentional changes like addition of newline were also there in the file.

To get rid of the unintentional changes I did git checkout -- and just copied only those changes back to the file and before doing git add I again did git diff on the same file it showed the same git diff status which it had shown when I first did the git diff with all the unintentional changes.

How to get out of this?

Answer Source

Are you using an IDE? It is possible that your editor has settings to clean up whitespace, which can cause messy commits. Check your editor's options; any that offer this feature should also allow it to be turned off.

To fix with git:

  • git reset to unstage changes
  • git add -p [file] to do a partial add; skip any hunks that you do not want to commit
  • git commit
  • git checkout . to get rid of unstaged changes