threads / discuss / 54514

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

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

## tl;dr

One message between Oct 27, 2020 and Oct 27, 2020.

replies: 0people: 1as markdown or json

Baptiste Fontaine· Oct 27, 2020, 12:39 UTC · lore
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

← back to recent threads