Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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

like image 455
Poomalairaj Avatar asked Jul 26 '11 11:07

Poomalairaj


People also ask

Can I switch branch with untracked files?

Take away: changing branches does not touch/change/remove untracked or checked in files. Remember that the working directory and index are not 'cleared' before the branch content is loaded into it!

Does git checkout affect untracked files?

git checkout does not affect untracked files. Git only manages tracked files, and it works fairly hard to avoid letting you lose data (which is critical).

Is checkout overwritten?

The Git “Your local changes to the following files would be overwritten by checkout” error occurs when you make changes on two branches without committing or stashing those changes and try to navigate between the branches. You can fix this issue by either stashing your changes for later or adding them to a commit.


1 Answers

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.

like image 181
Krishna Shasankar Avatar answered Sep 28 '22 08:09

Krishna Shasankar