Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I view multiple git diffs side by side in vim

Tags:

git

vim

diff

I'd like to be able to run a command that opens up a git diff in vim, with a tab for each file in the diff set.

So if for example I've changed files foo.txt and bar.txt in my working tree and I ran the command I would see vim open with two tabs. The first tab would contain a side-by-side diff between foo.txt in my working tree and foo.txt in the repository, and the second tab would contain a side-by-side diff for bar.txt.

Anyone got any ideas?

like image 243
Pete Hodgson Avatar asked Mar 11 '10 19:03

Pete Hodgson


People also ask

How do I view changes side by side in git?

You're taken to a page that shows the diffs as inline or unified for the file. Fortunately, there's a split button in the upper right hand corner that says Unified | Split. Clicking on Split portion of the button will show the before and after changes side by side, which is just my personal preference.

How do I see differences between files in git?

The git diff command displays the differences between files in two commits or between a commit and your current repository. You can see what text has been added to, removed from, and changed in a file. By default, the git diff command displays any uncommitted changes to your repository.

How do I see git diff in terminal?

The diff can be done with git diff (followed by the filename or nothing if you want to see the diff of all modified files). But if you already did something like git add * , you have to undo with git restore --staged .


2 Answers

Adapted Benjamin Bannier + Dave Kirby's answer above for fugitive users.

Because I use fugitive.vim, I adapted the above for my most frequent use case, looking at the diff between the last 2 commits:

vim -p $(git diff --name-only HEAD~1 HEAD) -c "tabdo :Gdiff HEAD~1"

Loading all the changes into tabs is so much better than going through them sequentially with git difftool.

like image 77
Kurt MacDonald Avatar answered Sep 27 '22 21:09

Kurt MacDonald


The way I would do this (though it isn't a single command)

  1. Open files with changes in new vim tabs:

    vim -p $(git diff --name-only)

  2. For every buffer get the diff to your current HEAD with the vcscommand vim plugin

    :VCSVimDiff

This gives a nice view of the difference, though not in patch form.

For anything else I would stick to git diff.

EDIT

Like Dave writes below, steps 1 and 2 can be combined by using

vim -p $(git diff --name-only) -c "tabdo VCSVimDiff"
like image 39
Benjamin Bannier Avatar answered Sep 27 '22 21:09

Benjamin Bannier