Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git revert all changes that are ignored by git diff -w

Tags:

git

git-diff

diff

When I do

git diff -w file_name

I see only added lines but when I remove -w I see lots of removed and re-added lines too.

I want to change the file so that I will show only the changes that are not ignored by -w option.

Is there a command for that?

like image 746
Narek Avatar asked Mar 17 '15 08:03

Narek


2 Answers

jupp0r was on the right track. First, cd to the root of your repository. Then:

git commit -a -m 'Backup commit.'
git branch pre-patch
git reset --hard HEAD~
git diff --patch -w HEAD pre-patch > patch.diff
git apply patch.diff

I'm not sure whether this will work for binary changes. If not, you can commit those separately beforehand. If this process fails, your code is in the pre-patch branch.

What this does:

  • Creates a commit and a branch to store the full changes. This also serves as a backup.
  • Goes back a step, to before those changes were made.
  • Gets the "diff -w" from the old code to the new code, formatted as a patch.
  • Applies the patch.

Note: If you've already committed the change and want to modify it, just omit the first step.

like image 153
piojo Avatar answered Nov 20 '22 13:11

piojo


You can do

git diff --no-color > stage.diff && git apply -R stage.diff && git apply --whitespace=fix stage.diff && rm -f stage.diff

If you haven't committed any changes yet.

like image 31
jupp0r Avatar answered Nov 20 '22 13:11

jupp0r