git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [suggestion] Fail cherry-pick if it overwrites untracked files with the matching file names

From
Elijah Newren <newren@gmail.com>
Date
Oct 16, 2022, 02:07 UTC
Message-ID
<CABPp-BG=pm8gfRZNW8Rk1V77s40Do1FZy0mxH6epubHbzF8U5w@mail.gmail.com>
In-Reply-To
<xmqq7d11540b.fsf@gitster.g>

On Sat, Oct 15, 2022 at 12:23 PM Junio C Hamano <gitster@pobox.com> wrote: [...]

Show 23 quoted lines
> Files in a working tree controlled by git fall into one of three
> classes.  Tracked files are those that are known to the index and
> appear in "git ls-files" output.  Among the others, ignored files
> are those that .gitignore mechanism says are expendable.  The rest
> are "untracked", possibly containing valuable contents that should
> not be lost as the user may choose to 'git add' them later.
>
> Not just cherry-pick but any merge-related operations, including
> "checkout", follow this semantics.  Untracked files are kept, but
> ignored files are expendable and will be removed if they are in the
> way to complete the operation the user asks.
>
>     $ rm .gitignore
>     $ git checkout master
>     error: The following untracked working tree files would be overwritten by checkout:
>             file
>     Please move or remove them before you switch branches.
>     Aborting
>
>     $ echo file >.gitignore
>     $ git checkout master
>     ... this should succeed, removing "file" whose contents were
>     ... marked expendable.

There is a command line option meant to allow tweaking this behavior: --[no-]overwrite-ignore. The default, as Junio explains above, is --overwrite-ignore. The --no-overwrite-ignore option works for `git checkout`. It also can work for `git merge`, but *only* if the merge results in a fast-forward. Although there is code in unpack_trees() to handle this alternate behavior by just setting a simple flag, we don't bother setting that flag in most merge paths even when the user specifies that option. It was just never hooked up. And several other related commands (am, reset, stash, rebase) don't even accept such a flag even though for consistency they probably should.

I've been meaning to get back to it and hook up this command line flag
for the occasional user that wants this alternative behavior.
However, it's not an issue for me or the direct users I support, and
complaints about this behavior from the community are quite rare too,
so I just haven't been too motivated to work on it.  If someone else
wants to take a stab, it should be pretty easy.  Just grep for
"preserve_ignored.*FIXME".  And maybe read up on these two commits for
context:
    04988c8d18 ("unpack-trees: introduce preserve_ignored to
unpack_trees_options", 2021-09-27)
    1b5f37334a ("Remove ignored files by default when they are in the
way", 2021-09-27)

Maybe I should just tag this as #leftoverbits to see if someone else picks it up?

Previous: Junio C HamanoNext: Junio C Hamano
Message 6 of 7 in “[suggestion] Fail cherry-pick if it overwrites untracked files with the matching file names”
  1. Rupinderjeet SinghOct 15, 2022
  2. Junio C HamanoOct 15, 2022
  3. Junio C HamanoOct 15, 2022
  4. Rupinderjeet SinghOct 15, 2022
  5. Junio C HamanoOct 16, 2022
  6. Elijah NewrenOct 16, 2022
  7. Junio C HamanoOct 16, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.