4

I'd like to compare two files, one file is in another branch and the other is the uncommitted version of the same file in the current branch. So I do :

git difftool other_branch_name file_to_compare

now I want to edit uncommitted version in the current branch but I can't because vimdiff is opened in read-only mode...

So what's the solution to use git difftool and allow modification ?

PinkFloyd
  • 2,103
  • 4
  • 28
  • 47
  • 2
    See http://stackoverflow.com/questions/2099210/vimdiff-multiple-windows-different-read-write-permission for a hint. – mah Jul 14 '13 at 11:38
  • looks good but how can i configure difftool to use this option ? – PinkFloyd Jul 14 '13 at 11:43
  • 1
    I don't know if you can configure it (perhaps you can, but I'll assume not), but you can change the mode on the fly as long as you can hit escape to get to command mode. – mah Jul 14 '13 at 11:51
  • 1
    I know this is an old question, but for me, usually I just let 'git difftool' just open vimdiff for me in RO mode, and _IF_ I see that I need to edit the target file, I just do ':set noreadonly' and the "[RO]" status goes away and then, I can edit and save... – HidekiAI Feb 12 '17 at 03:20
  • @HidekiAI thanks! that's what I ended up doing... – PinkFloyd Feb 12 '17 at 15:17

2 Answers2

4

I don't know the specific commands necessary to invoke vimdiff so that the uncommitted file is opened in write mode, but in general, git does in fact have configuration settings that you can use to set up how your difftool if invoked when you use git difftool. See the difftool.<tool>.cmd setting in the git difftool doc and the git config doc:

difftool.<tool>.cmd

Specify the command to invoke the specified diff tool. The specified command is evaluated in shell with the following variables available: $LOCAL is set to the name of the temporary file containing the contents of the diff pre-image and $REMOTE is set to the name of the temporary file containing the contents of the diff post-image.

So you could do something like this to configure your difftool:

$ git config --global difftool.myDifftool.cmd \
"myDifftool --read-only-flag $LOCAL --editable-flag $REMOTE"

Based on the link to Vimdiff multiple windows different read/write permission that mah posted, I guess you could configure difftool for vimdiff this way, but I'm not sure if the vimdiff options are actually correct:

$ git config --global difftool.vimdiff.cmd \
"vimdiff -R $LOCAL -c ':se noreadonly' $REMOTE"
Community
  • 1
  • 1
3

Why not just create a temporary file of the other version:

git show other_branch_name:file_to_compare > diff.file_to_compare
vimdiff file_to_compare diff.file_to_compare
rm -rf diff.file_to_compare
cforbish
  • 8,567
  • 3
  • 28
  • 32