0

let say in a repo there are commit1 and after that commit2.

commit2 has .gitignore file changed, and some files ignored.

If I checkout to commit1, those ignored files becomes untracked files and prevent me to checkout back to commit2.

What would be the best way to checkout back to commit2, without touching those ignored files?

git stash would delete those files from workspace first, which I don't prefer.

somebody4
  • 505
  • 4
  • 14
  • 1
    Please show the sequence of commands that you use to switch from commit1 and back to commit2. In particular, show the error message(s). I'm asking because the way that you describe the situation I do not see how Git would disallow to switch back to commit2. Did you perhaps not only switch back and forth, but also create the untracked file while you were on commit1? – j6t Aug 04 '23 at 06:32

1 Answers1

2

If a file is already tracked, adding its name to .gitignore is not enough to have git stop tracking it, you need to explicitly remove it from the versioned files.

See for example: How do I make Git forget about a file that was tracked, but is now in .gitignore?


Check if commit2 contains that file :

# if output is empty: the file is not there
git ls-tree <commit2> -- path/to/file
LeGEC
  • 46,477
  • 5
  • 57
  • 104