# [BUG] merge-ort: --no-overwrite-ignore overwrites an ignored file at a directory-rename destination

3 messages from 2026-10-01 to 2026-11-19. Participants: Coy Geek, Aniket Gupta.
Thread: https://gitlist.dev/t/66440

## Coy Geek, 2026-10-01 23:54

Subject: [BUG] merge-ort: --no-overwrite-ignore overwrites an ignored file at a directory-rename destination
Message-ID: <CACgTecNrMKHGexWTkm1bqpVzL7SVWD5Hwo17wVokVGe3_Rnx0A@mail.gmail.com>
URL: https://gitlist.dev/e/CACgTecNrMKHGexWTkm1bqpVzL7SVWD5Hwo17wVokVGe3_Rnx0A%40mail.gmail.com

```
Hello,

Consider the following ...

`git merge --no-overwrite-ignore` is documented to abort rather than
overwrite ignored files. When directory rename detection moves a file
that was added on the current branch into a path that holds an ignored,
untracked file, the merge replaces that file's contents without any
warning.

With merge.directoryRenames=true the merge reports success (exit 0).
With the default, merge.directoryRenames=conflict, it stops with a "file
location" conflict, but the ignored file has already been overwritten. A
direct collision with the same ignored path is correctly refused, so the
protection is bypassed only for the destination path that rename
detection generates.

What did you do before the bug happened? (Steps to reproduce your issue)

Save the script below as repro.sh and run it as "sh repro.sh true", "sh
repro.sh conflict", or "sh repro.sh default" (no -c override). It works
in a throwaway repository with empty global and system configuration.

    #!/bin/sh
    set -eu
    mode=${1:-true}
    unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_COMMON_DIR
GIT_CONFIG_PARAMETERS
    export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null
    tmp=$(mktemp -d)
    cd "$tmp"
    git init -q -b main repo
    cd repo
    git config user.name Example
    git config user.email example@example.invalid
    echo new/private.dat >.gitignore
    mkdir old
    echo seed >old/seed
    git add .gitignore old/seed
    git commit -qm base
    git checkout -qb incoming
    git mv old new
    git commit -qm 'rename old/ to new/'
    git checkout -q main
    echo 'committed local bytes' >old/private.dat
    git add old/private.dat
    git commit -qm 'add old/private.dat'
    mkdir new
    echo 'unique ignored bytes' >new/private.dat
    git status --porcelain --ignored
    git version
    status=0
    if [ "$mode" = default ]; then set -- ; else set -- -c
merge.directoryRenames="$mode"; fi
    git "$@" merge --no-edit --no-overwrite-ignore incoming || status=$?
    echo "merge exit status: $status"
    echo "new/private.dat now contains: $(cat new/private.dat)"

Before the merge, `git status --porcelain --ignored` reports only "!!
new/": the worktree is clean, and new/private.dat is ignored and
untracked.

What did you expect to happen? (Expected behavior)

As for any other merge result that would overwrite an ignored file under
--no-overwrite-ignore, Git refuses before touching the worktree, names
new/private.dat, and leaves its contents as "unique ignored bytes".

What happened instead? (Actual behavior)

With merge.directoryRenames=true:

    Path updated: old/private.dat added in HEAD inside a directory
that was renamed in incoming; moving it to new/private.dat.
    Merge made by the 'ort' strategy.
     {old => new}/private.dat | 0
     {old => new}/seed        | 0
     2 files changed, 0 insertions(+), 0 deletions(-)
     rename {old => new}/private.dat (100%)
     rename {old => new}/seed (100%)
    merge exit status: 0
    new/private.dat now contains: committed local bytes

With the default configuration, or merge.directoryRenames=conflict:

    CONFLICT (file location): old/private.dat added in HEAD inside a
directory that was renamed in incoming, suggesting it should perhaps
be moved to new/private.dat.
    Automatic merge failed; fix conflicts and then commit the result.
    merge exit status: 1
    new/private.dat now contains: committed local bytes

In both cases the ignored bytes are lost. They are not in any commit, in
the index, or in a stash, so `git merge --abort` cannot restore them.

What's different between what you expected and what actually happened?

Control case: if "incoming" instead adds new/private.dat directly (with
`git add -f`, no rename involved), the same `git merge --no-edit
--no-overwrite-ignore incoming` aborts with exit 1 and leaves "unique
ignored bytes" intact:

    error: The following untracked working tree files would be
overwritten by merge:
            new/private.dat
    Please move or remove them before you merge.
    Aborting

So --no-overwrite-ignore works for a direct collision and fails only
when the destination is produced by directory rename detection in the
ort strategy.

This matters because --no-overwrite-ignore is the only merge-level guard
for ignored content such as local configuration, credential files, or
build inputs that users deliberately keep out of history. A user or tool
relying on it can lose unique, unversioned data, with no error at all or
with only an unrelated rename conflict message.

Anything else you want to add:

Reproduced with identical results, in both modes, on:

    git version 2.56.0.138.gc61827130   (built from next)
    git version 2.56.0.50.gc46c1e3772   (built from master)
    git version 2.56.0                  (Homebrew)
    git version 2.55.0                  (Homebrew)
    git version 2.54.0 (Apple Git-157)

all on macOS 27 (arm64, APFS), using the empty-configuration environment
shown in the script. I am happy to test a patch.

Regards, CoyGeek

```

## Aniket Gupta, 2026-11-07 04:44

Subject: [PATCH] merge: honor --no-overwrite-ignore for real merges
Message-ID: <20261107044419.209119-1-aniketgupta3001@gmail.com>
URL: https://gitlist.dev/e/20261107044419.209119-1-aniketgupta3001%40gmail.com
In-Reply-To: <CACgTecNrMKHGexWTkm1bqpVzL7SVWD5Hwo17wVokVGe3_Rnx0A@mail.gmail.com>

```
"git merge --no-overwrite-ignore" is meant to abort instead of
overwriting an ignored file that is in the way. However, only a
fast-forward honors it. When a real merge is needed, ort and recursive
overwrite the ignored file whatever the option says. Two FIXME comments
in merge-ort.c and builtin/merge.c mark where it was never passed along.
Add a preserve_ignored bit to struct merge_options then set it from the
option and use it where merge-ort updates the working tree. The
trivial-merge path in builtin/merge.c gets the same treatment. Other
callers of merge-ort leave the bit unset so nothing will change for
them.

Add tests for a plain merge and for a directory rename.

Reported-by: Coy Geek <coygeek@gmail.com>
Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com>
---
 builtin/merge.c  |  3 ++-
 merge-ort.c      |  2 +-
 merge-ort.h      |  1 +
 t/t7600-merge.sh | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/builtin/merge.c b/builtin/merge.c
index 5b4eb23a83..c7cd3ce29e 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -756,7 +756,7 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head,
 	opts.verbose_update = 1;
 	opts.trivial_merges_only = 1;
 	opts.merge = 1;
-	opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
+	opts.preserve_ignored = !overwrite_ignore;
 	trees[nr_trees] = repo_parse_tree_indirect(the_repository, common);
 	if (!trees[nr_trees++])
 		return -1;
@@ -812,6 +812,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
 		}
 
 		init_ui_merge_options(&o, the_repository);
+		o.preserve_ignored = !overwrite_ignore;
 		if (!strcmp(strategy, "subtree"))
 			o.subtree_shift = "";
 
diff --git a/merge-ort.c b/merge-ort.c
index c410a5d353..38dd1ac99d 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -4632,7 +4632,7 @@ static int checkout(struct merge_options *opt,
 	unpack_opts.quiet = 0; /* FIXME: sequencer might want quiet? */
 	unpack_opts.verbose_update = (opt->verbosity > 2);
 	unpack_opts.fn = twoway_merge;
-	unpack_opts.preserve_ignored = 0; /* FIXME: !opts->overwrite_ignore */
+	unpack_opts.preserve_ignored = opt->preserve_ignored;
 	if (repo_parse_tree(opt->repo, prev) < 0)
 		return -1;
 	init_tree_desc(&trees[0], &prev->object.oid, prev->buffer, prev->size);
diff --git a/merge-ort.h b/merge-ort.h
index 6045579825..d5747f358e 100644
--- a/merge-ort.h
+++ b/merge-ort.h
@@ -85,6 +85,7 @@ struct merge_options {
 	unsigned renormalize : 1;
 	unsigned mergeability_only : 1; /* exit early, write fewer objects */
 	unsigned record_conflict_msgs_as_headers : 1;
+	unsigned preserve_ignored : 1; /* do not overwrite ignored files */
 	const char *msg_header_prefix;
 
 	/* internal fields used by the implementation */
diff --git a/t/t7600-merge.sh b/t/t7600-merge.sh
index 64fe21717d..c1150e50e0 100755
--- a/t/t7600-merge.sh
+++ b/t/t7600-merge.sh
@@ -1179,4 +1179,63 @@ test_expect_success 'merge with no argument defaults to upstream' '
 	)
 '
 
+test_expect_success 'merge --no-overwrite-ignore keeps an ignored file in the way' '
+	test_when_finished "rm -rf ignored" &&
+	git init ignored &&
+	(
+		cd ignored &&
+		echo secret >.gitignore &&
+		git add .gitignore &&
+		test_tick &&
+		git commit -m base &&
+		git checkout -b side &&
+		echo side >secret &&
+		git add -f secret &&
+		test_tick &&
+		git commit -m side &&
+		git checkout - &&
+		test_commit other &&
+		echo mine >secret &&
+
+		test_must_fail git merge --no-overwrite-ignore side &&
+		echo mine >expect &&
+		test_cmp expect secret &&
+		test_path_is_missing .git/MERGE_HEAD &&
+
+		git merge --overwrite-ignore side &&
+		echo side >expect &&
+		test_cmp expect secret
+	)
+'
+
+test_expect_success 'merge --no-overwrite-ignore with a directory rename' '
+	test_when_finished "rm -rf ignored" &&
+	git init ignored &&
+	(
+		cd ignored &&
+		echo new/private >.gitignore &&
+		mkdir old &&
+		echo seed >old/seed &&
+		git add .gitignore old/seed &&
+		test_tick &&
+		git commit -m base &&
+		git checkout -b incoming &&
+		git mv old new &&
+		test_tick &&
+		git commit -m "rename old to new" &&
+		git checkout - &&
+		echo committed >old/private &&
+		git add old/private &&
+		test_tick &&
+		git commit -m "add old/private" &&
+		mkdir new &&
+		echo mine >new/private &&
+
+		test_must_fail git merge --no-overwrite-ignore incoming &&
+		echo mine >expect &&
+		test_cmp expect new/private &&
+		test_path_is_missing .git/MERGE_HEAD
+	)
+'
+
 test_done
-- 
2.43.0



```

## Aniket Gupta, 2026-11-19 17:19

Subject: Re: [PATCH] merge: honor --no-overwrite-ignore for real merges
Message-ID: <20261119171954.85707-1-aniketgupta3001@gmail.com>
URL: https://gitlist.dev/e/20261119171954.85707-1-aniketgupta3001%40gmail.com
In-Reply-To: <20261107044419.209119-1-aniketgupta3001@gmail.com>

```
Friendly ping. Elijah could you pls take a look when possible?


```
