Reputation: 2990
When doing a git-merge
or git-pull
with local untracked files that have been added remotely, the following error appears
error: The following untracked working tree files would be overwritten by merge:
Many questions have been asked about this, but all that I found wanted to overwrite the local files. However, I want to get a diff, i.e. merge the files to make sure nothing gets lost.
How could I achieve that automatically, without processing the files one by one?
Upvotes: 0
Views: 1264
Reputation: 488453
A merge, in Git, needs three—not just two—files:
If you have an untracked file in your working tree, and they have a file of the same name in their commit, that's your file and their file. Where is the base file?
The git merge
command won't make one up, but you can do the following:
foo
to foo.ours
);git merge
run and produce a new merge commit (resolving conflicts if needed); thenYou now have three files:
foo.base
: you just came up with this yourself.foo
: this is their version of the file.foo.ours
: this is the one you had before you started the git merge
that you have now finished.You can now:
foo
to foo.theirs
;foo.ours
to foo
;git merge-file foo foo.base foo.theirs
This git merge-file
uses the same algorithm as git merge
, but takes the three files directly. The ours
version is the one you name first: foo
here. The merge result is written back to this file, so it's a good idea to use a copy, in case Git makes a mess of it and you decide you would rather start over.
Note that you may, if you wish, use an empty file as the merge base. If you do this, though, the result is usually not very helpful. If you decide to do that anyway, you can simply add and commit your file before merging: git merge
will see the two copies of foo
as both added, i.e., an add/add conflict
, and do the same thing that all this fussing-about with git merge-file
would have done.
Upvotes: 2
Reputation: 37752
I see two options for you:
git merge
git merge
will put the new files in place.git diff
will show you now the differences between your version and the version you just merged in.Upvotes: 0