10

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?

Narek
  • 38,779
  • 79
  • 233
  • 389
  • Maybe using a script similar to the pre-commit hook I mentioned in http://stackoverflow.com/a/592014/6309? – VonC Mar 17 '15 at 09:06
  • @VonC : I would be very grateful if you could please that script for us. – Abhishek Anand Jul 25 '15 at 07:27
  • @Abhishek do you mean like this one? https://github.com/imoldman/config/blob/master/pre-commit.git.sh – VonC Jul 25 '15 at 07:31
  • Because my file is from a shared repository, I want to minimize the changes I commit/push to it. I want a script which reverts a change (at the granularity of individual lines) if and only if it is a mere-line-ending change. Thanks. – Abhishek Anand Jul 25 '15 at 07:42

2 Answers2

6

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.

piojo
  • 6,351
  • 1
  • 26
  • 36
1

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.

jupp0r
  • 4,502
  • 1
  • 27
  • 34