2

Is there a way to commit only the lines with non-white-space changes?

I have (add-hook 'before-save-hook 'delete-trailing-whitespace) in my .init.el and this usually helps me avoid committing trailing white spaces.

However, when I'm working on a project that does not care about those trailing white spaces, the setting makes a huge diff on files I've saved. It might be possible to make the setting per project, but I'm looking for a way to tell Magit to handle it.

I've found a similar question for git proper at https://stackoverflow.com/questions/3515597.

git diff has a variety of options for white spaces --ignore-space-at-eol, --ignore-space-change, --ignore-all-space, and --ignore-blank-lines. It would be nice if we could select those options at magit's popup window.

3
  • This is something that needs to be supported in git as you have found in the linked question. Obviously in magit you can run any git command so you can set up the alias from the linked answer. However I think you need to talk to the other people on the project (maybe that is just talking to yourself) and decide if trailing whitespace is bad. If the other people accept it then remove your hook, as changing the whitespace is a change! Otherwise do your change as 2 commits, the first to remove the trailing space and then whatever change that prompted editing those files.
    – icarus
    Jan 9, 2017 at 7:49
  • 1
    You might want to look at github.com/lewang/ws-butler - "Unobtrusively trim extraneous white-space ONLY in lines edited."
    – npostavs
    Jan 9, 2017 at 14:44
  • Yes, please use ws-butler or ws-trim. You shouldn't be touching whitespace on lines you haven't edited. (add-hook 'before-save-hook 'delete-trailing-whitespace) is a bad solution.
    – phils
    Jan 10, 2017 at 11:59

1 Answer 1

3

Some of these options are already available in the diff popup. If you need any of the others, then you have to add them as described in the manual.

The next issue you will run into is that if you save the default arguments using C-x C-s, that will affect all repositories. There currently is no mechanism for saving the arguments for just one repository.

But you could set magit-diff-arguments and magit-diff-section-arguments manually in .dir-locals.el as described in the manual. That's a bit cumbersome but it's all we have now.

1
  • Ah, I didn't know that activated diff switches also affects magit-stage as well as magit-diff-*!! Thanks! Jan 10, 2017 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.