git checkout –patch by words?

We have a project where the files contain unfortunately long lines, with no
possibility of shortening them. We could streamline our workflow significantly if
we could use git checkout --patch also with such files. This does not work since
if there is any change in the whole huge line, the line gets flagged as changed.

Usually, we inspect such changes using git diff --word-diff. Is there a possibility
to make checkout --patch work with similar format? Are there any other means to
work around our problem?

  • vimdiff between current and previous git version of a file
  • Vim ignore specifc file in autocommand
  • Git: A ton of blank lines showing up in diff when I haven't modified them
  • Could not execute editor
  • gvimdiff mergetool for msysgit
  • Git: Cloning through symlink requires password?
  • Emacs shell: save commit message
  • Restore pathogen git repos
  • One Solution collect form web for “git checkout –patch by words?”

    After some trying around, I got this answer to work and figured out how to use it for your checkout. I made some additional changes to squelch a warning that would sometimes be printed and had to fix the argument parsing to be a bit more flexible. (Since this is originally a plumbing script where the arguments get pre-processed by the git core, this is not necessary in the original version).

    You can use this version of the file (raw file for save-link-as convenience) and copy it to somewhere in your PATH, as described in the linked answer. Be sure to also set its executable bit (chmod +x path/to/file) after downloading.

    Assuming you named the file git-add--interactive--words as suggested, you can use the following command to define an alias for it:

    git config --global alias.cop add--interactive--words --patch=checkout
    

    Now you can do something like:

    git cop HEAD~5 to do an interactive checkout of . (current dir) 5 commits ago, or
    git cop master -- docs/README to interactively check out docs/README from branch master

    You can call the alias anything you want, of course (I chose cop here for ‘c‘heck’o‘ut –‘p‘atch).

    I know this answer is really late so it’s possibly not applicable for you anymore, but this question has been on the unanswered list for so long that I just had to figure it out today — maybe it’ll help someone else 😉

    Git Baby is a git and github fan, let's start git clone.