Alex
Alex

Reputation: 2402

Git - apply change made in the difftool

I'm sure this is very basic, but I've read the docs, and still can't figure it out.

I have two branches, and I need to merge some changes in a couple of files in manually (don't ask). I am viewing the files side by side, with the difference highlighted, with:

git difftool branch1 branch2 -- path_to_file

This allows me to copy everything I need into the file that will be remaining (which is in branch1 fwiw), but the file is some long filename in /tmp/. I don't know how to actually save my edits to the file that I am editing (I'm using vim as the difftool, and I have set noreadonly, the issue is that it's a different file (something in /tmp/, as I said).

There are only a couple of changes, and I could just manually edit the file in branch1, but I'm sure that git diff must allow me to do this, and I'd like to learn to do it properly.

Upvotes: 1

Views: 543

Answers (2)

nvd
nvd

Reputation: 3371

A function can help. Tested with ZSH and may require adaption for other shells. Can be used on the whole repo or a path can also be specified to narrow down the scope.

gitdifftoolbranch() {
    TARGET_BRANCH_REMOTE="origin/master"

    if [ -z "$1" ]; then
        local files=("${(f)$(git diff --name-only --line-prefix=$(git rev-parse --show-toplevel)/ ${TARGET_BRANCH_REMOTE}...)}")
    else
        local files=("${(f)$(git diff --name-only --line-prefix=$(git rev-parse --show-toplevel)/ ${TARGET_BRANCH_REMOTE}... -- "$1")}")
    fi

    for file in ${files}
    do
        git difftool ${TARGET_BRANCH_REMOTE} -- ${file}
    done
}

Upvotes: 0

Alex
Alex

Reputation: 2402

I can just force vim to overwrite the original file when it saves... I guess this works fine.

Upvotes: 0

Related Questions