user842225
user842225

Reputation: 5989

git complains about untracked file

I have two branches:

-- feature
-- master

I used to work on feature branch. Then, I changed to master branch by git checkout master. After I worked on master, I run git status , which shows me the following:

# On branch master
# Your branch is ahead of 'origin/master' by 38 commits.
#   (use "git push" to publish your local commits)
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   MyProject/school.java
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   MyProject/other/proj.properties

I decide to omit the changes I made on MyProject/school.java , so I run:

git checkout MyProject/school.java , it works well.

At this point, I run git checkout feature to go back to feature branch, but I got the following error message:

error: The following untracked working tree files would be overwritten by checkout:
    MyProject/other/proj.properties

I tried git rm --cached MyProject/other/proj.properties but I got the following error:

fatal: pathspec 'MyProject/other/proj.properties' did not match any files

Upvotes: 2

Views: 3351

Answers (1)

toydarian
toydarian

Reputation: 4554

The reason is, that the file is tracked in the feature branch, but not in master. So the the file in master would be overwritten with the one in feature by the checkout and you would loose your changes.

Git recognizes that and prevents you from loosing the changes in the untracked file.

Edit: If the changes in that file can be removed, just delete the file. You do not need git rm as the file is not tracked.

Upvotes: 2

Related Questions