Ivan
Ivan

Reputation: 15922

Why do I receive the error "Untracked working tree file" when switching branches in Git?

Until yesterday my project has only a branch with a .gitignore file similar to:

*.log
upload/*
!upload/global/empty.txt

It worked fine until I added a new branch and changed this .gitignore to not ignore upload files. The new .gitignore file remained with only one line:

*.log

I returned to master branch without problems, but now each time I want to go to the new branch I receive a message like this:

error: Untracked working tree file 'upload/file.txt' would be overwritten by merge.

I need to checkout this branch to work with it or, at least, rescue the commits I made.

Note: I've found this similar question, but doesn't answer my question.

Upvotes: 3

Views: 2712

Answers (1)

Andy
Andy

Reputation: 46384

Your issue is that in the new branch you are not tracking upload/file.txt but you are tracking it in your master branch. So when you switch to your new branch, your version, from master, will overwrite the untracked file.

Add the -f flag, to your git checkout, to force git to overwrite the untracked file.

Upvotes: 9

Related Questions