Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git internals: Modifying `git-merge-one-file` to not use working tree

Tags:

git

Is it possible to modify the default git-merge-one-file program to do everything in the index without touching the working tree, leaving it completely unmodified?

UPDATE AND DETAILS

So I understand now that a file-level merge (where the merge is acting on lines in the file rather than whole files) can't occur without using a worktree. (Unlike a merge acting on whole files.) So I'm gonna have to use a worktree.

Another detail: I'm okay with the solution working only in the cases where the merge can be done automatically without manual resolution. It's okay if it just shows an error message if the merge is not automatic. (And of course, leave everything clean.)

Another detail: I'm not using git-merge-one-file directly, I'm using it inside this script: https://gist.github.com/cool-RR/6575042

I tried to follow @torek 's advice and use a temporary work tree (as you can see in the script), because that seems like the best direction so far. Problem is, I get these errors:

git checkout-index: my_file is not in the cache
error: my_file: cannot add to the index - missing --add option?

I googled these error messages but couldn't find anything helpful.

Any idea what to do?

like image 540
Ram Rachum Avatar asked Sep 13 '13 12:09

Ram Rachum


People also ask

How do you abort a merge?

You can use the git reset --merge command. You can also use the git merge --abort command. As always, make sure you have no uncommitted changes before you start a merge.

Does git merge changes both branches?

Merging Branches. Once you've completed work on your branch, it is time to merge it into the main branch. Merging takes your branch changes and implements them into the main branch. Depending on the commit history, Git performs merges two ways: fast-forward and three-way merge.

Does git merge overwrite?

Usually git does not overwrite anything during merge.


1 Answers

While git needs a place to do its work, you could point it off to a different work-tree location for the duration of the "merge one file" op.

I have no idea if/how this works "out of the box" for merge-one-file, but the env variable to set is GIT_WORK_TREE:

env GIT_WORK_TREE=/some/where/else git ...

(you can leave out env with most, but not all, shells).

A more or less equivalent method that might "feel safer" :-) or be more convenient for some purposes is to work in the other directory, and use GIT_DIR to the location of the repo:

cd /some/where/else
env GIT_DIR=/place/with/repo/.git git ...

You can even combine them, setting both GIT_DIR and GIT_WORK_TREE.

like image 76
torek Avatar answered Oct 27 '22 23:10

torek