Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

git says "The following untracked working tree files would be overwritten by checkout" when switching branches

I have a branch called develop in remote bare repo hub, and I have a checkout copy of the develop branch in my local git repository (user poomalai).

I have deleted (git rm --cached) a file (Mydirectory/myfile.php) from another checkout copy (user raj), and pushed to the hub repository.

Now, when I pull the changes from the hub to (user poomalai), it works fine, and I have the commit message file deletion in the git log.

I have added the file in the .gitignore file in the (user poomalai).
Now I have created the file in my local repo (user poomalai).

Now the file is no longer in the version control, and changes to the file is not tracked by git.

But when I try to switch to some other branch it says the following:

error: The following untracked working tree files would be overwritten by checkout:
Mydirectory/myfile.php
Please move or remove them before you can switch branches.
Aborting

When I remove the file using linux rm command, I am able to switch branches.
If I create the file again, it again throws the same error.
I need the file to be in the directory but it should not be tracked by git.

I tried following commands:

git rm  

fatal: pathspec 'diamonds_webservice/dbconnect.php' did not match any files

git gc
git reset --hard HEAD
git pull

Nothing works. Please someone help me

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This could be possible because the the file is added to the repo in the 'target branch'.
Ensure that the file is git rm'ed from the 'target branch', and then try switching branches.

As @CharlesB said, git does it to ensure data integrity, so that nothing is lost when switching directories.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...