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?
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.
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.
Usually git does not overwrite anything during merge.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With