Max
Max

Reputation: 860

Git: Cannot remove .php~ files

I use Git on a Symfony 2 project. I would like to switch branches, but I get the following message:

These untracked working tree files would be overwritten by checkout. Please move or remove them before you can checkout.

Here a list of the files:

srv/myproject/src/mycompany/mybundle/Entity/Contacts.php~
srv/myproject/src/mycompany/mybundle/Entity/Products.php~
srv/myproject/src/mycompany/mybundle/Entity/Orders.php~

What I tried so far: For example, I tried to do

git rm --cached srv/myproject/src/mycompany/mybundle/Entity/Contacts.php~

which gives me the message that the file does not exist.

I cannot find any "php~" files in the "Entity"-Folder. I can only see the regular "php" files.

Why do these files have the char "~" at the end of the file name?

How do I solve this problem?

Upvotes: 0

Views: 187

Answers (2)

torek
torek

Reputation: 488203

Files with names ending with ~ are typically backup files made by some editors. Whether they are made by your editor, I have no idea.

They are shown as untracked (Git says these untracked working tree files...), which means they are not in the index, so you cannot give them to git rm --cached which just removes things from the index. This in turn means that—at least at this point—you have done nothing wrong. :-) Which might be reassuring, but is not really helpful.

Nonetheless, git checkout <otherbranch> is complaining that the proposed checkout would overwrite them. This means that they are contained in the commit that this git checkout would check out. That, in turn, means that someone, at some point, did something wrong, by committing them in the first place. (Assuming, at least, that they are just editor backup files that should not be versioned like this.) That could be someone other than you, meaning you have done nothing wrong, but, well, still not helpful.

This also means that they are in your work-tree right now. How they got there is hard to say, though the most obvious candidate reason is that your editor made them as editor backup files.

What you need to do

What you need to do is to move or remove these files (assuming they're editor backups, it is likely safe to just remove them entirely) so that they are no longer in your work-tree. Then git checkout will be able to put the ones from the commit into your work-tree, without clobbering the ones you already have, since you no longer have them.

What else you and/or others need to do

Committing these files is a bad idea (assuming, as always, that these are editor backups). It's kind of hard to fix this in existing commits. It's up to you and your co-workers / colleages whether to bother. However, one way or another, future commits should not contain these files.

To that end, you, or someone, must not only remove them now, you must also check out the commit(s) that have them, then remove them (e.g., git rm *~), then either fix those commits or make new commits that tell Git "they were there, and they will be in commit history, but from that commit to this new one, remove them now."

Moreover, you, or someone, should probably set things up so that Git ignores these files by default, so that it will not put them into future commits. The usual method is to add a line *~ to the top level .gitignore file. (However, once files are tracked, adding them to .gitignore has no effect until they become un-tracked by an explicit remove. This is because .gitignore is the wrong name: it's not a list of files to ignore, it's a list of files to not add, and to not complain about forgetting to add.)

Upvotes: 2

rokas
rokas

Reputation: 1581

  1. remove unwanted files from git:

    git reset HEAD path/to/unwanted_file

  2. create .gitignore file and add *~ to it.

  3. do a new commit.

Upvotes: 0

Related Questions