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
Junio C Hamano <gitster@pobox.com>
Date
Oct 15, 2022, 18:35 UTC
Message-ID
<xmqq7d11540b.fsf@gitster.g>
In-Reply-To
<CAAheMRzYX6PdWMtcB=px_kD=-gnGGHvDgvR6Jxy94JH2DpYpJw@mail.gmail.com>

If the situation is about cherry-picking a commit that adds a new file F to a checkout of another commit that lacks the file F, I think the command is working exactly as designed.

    $ git init
    $ git commit -m 'initial' --allow-empty
    $ git tag initial
    $ date >file
    $ git add file
    $ git commit -m 'add file'
    $ git tag added
    $ git checkout -b second initial
    ... at this point we are back to the original state
    ... without 'file'
    $ >file
    ... the file is untracked with precious contents
    ... and the presence of it stops a cherry-pick that clobbers it
    $ git cherry-pick added
    error: The following untracked working tree files would be overwritten by merge:
            file
    Please move or remove them before you merge.
    Aborting
    fatal: cherry-pick failed
Now continuing from the above, things get (slightly) interesting
    $ echo file >.gitignore
    $ git cherry-pick added

This will replace "file" with the one from the "added" commit, and that is because the user marked that the "file" in the working tree is expendable.

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.

Of course, after switching to 'master' (or cherry-picking 'added'), the project now cares about the 'file'. After all, it bothered to add it to keep track of the changes to its contents. So it is recommended that you would adjust the contents of .gitignore so that it no longer is marked as expendable.

Previous: Junio C HamanoNext: Rupinderjeet Singh
Message 3 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.