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

No warning when checking-out a branch with a file that already exists but is not indexed in the current branch

From
BFBaptiste Fontaine <b@ptistefontaine.fr>
Date
Oct 27, 2020, 12:39 UTC
Message-ID
<ED4DFC77-2135-476E-AA22-7E887B25565D@ptistefontaine.fr>
Hello,
I’m not sure if this is a bug or expected behavior:
1. Create a sample repo:
  % git init
  % touch a.txt
  % git add a.txt
  % git commit -m "add a.txt"
2. Create a new branch, in which you add foo.txt:
  % git checkout -b devel
  % echo "Something" > foo.txt
  % git add foo.txt
  % git commit -m "Write something in foo.txt"
3. Go back to the first branch and create an ignored file with the
   same name:
  % git checkout master
  % echo foo.txt > .gitignore
  % git add .gitignore
  % git commit -m "ignore foo.txt"
  % echo "Something else" > foo.txt
  % cat foo.txt
  Something else
4. Check-out the branch with the indexed file:
  % git checkout devel # <-- no warning here
  % cat foo.txt
  Something
5. Check-out again the branch without the indexed file:
  % git checkout master
  % cat foo.txt
  cat: foo.txt: No such file or directory

The non-indexed file is now gone, and there was no warning when git “took over it” in step 4. As far as I know there’s no way to retrieve the "Something else" content.

I’m using git version 2.28.0.

I came to this situation on a repository in which I had a ROADMAP.md file on master, which I `git rm --cached` and added to .gitignore on devel. I later modified this file, but it was removed when I switched on master.

Regards,
Baptiste Fontaine
Message 1 of 1 in “No warning when checking-out a branch with a file that already exists but is not indexed in the current branch”
  1. Baptiste FontaineOct 27, 2020

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.