13

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

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Poomalairaj
  • 4,888
  • 3
  • 23
  • 27

3 Answers3

9

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.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
  • 1
    Great. This works fine. So I have to git rm the file from all the branches which are created before the file deletion from the hub. Thanks – Poomalairaj Jul 26 '11 at 12:34
  • 4
    How do we "git rm" from a branch we can't checkout? – brazorf Nov 25 '15 at 23:44
  • 1
    So you indicate a theoretical idea of what could be done, but not *how* to achieve this. As written, this is not so much a useful answer, as a hint or comment. Please `edit` to show others less versed in git *how* to apply your suggestion to achieve a solution. Thanks! – SherylHohman Sep 05 '18 at 17:00
5

It's a good thing that git warns you about this, because switching to a branch that doesn't have the file deleted will... delete it.
So you're doing it right: remove (or rename) it before checking out a branch that has it deleted.

So it's just that git cares about your data before overwriting it.
Since it's unversioned you wouldn't be able to recover it after the checkout.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 1
    But it is a tedious task each and every time i should rename the file etc. Why doesn't git just ignore the file and dont track and dont abort the branch switch? – Poomalairaj Jul 26 '11 at 12:17
  • 1
    Because the branch switch will overwrite (or delete) unversioned data (your now-untracked-file) – CharlesB Jul 26 '11 at 12:28
  • http://stackoverflow.com/questions/7630693/git-warning-of-file-overwriting-due-to-supposedly-untracked-files you can use something like `rm git checkout xyz 2>&1 | sed "s/^[^\t].*/ /g" ` (where is shift-~) to automagically delete those files – Jamie Pate Mar 15 '13 at 16:46
  • 1
    @pooamlairaj Yes, this is because the file would ned to be removed from *every* branch, then this change needs to be committed. The `.gitignore` file is also part of the snapshot. I understand the tediousness, (though there are ways to automate the process), however, being able to save snapshots of the filesystem, such that changes in one branch (or duplicated repo), do *not* automatically (& silently) affect branches (and repo clones) is the Power of version control. It actually makes sense once you can wrap your mind around what's happening behind the scenes, and why. All the best. – SherylHohman Sep 05 '18 at 17:38
0

The only thing that worked for me:

in android studio - go to event log - click 'view them' hyperlink. - then manually hit the minus bar to delete the files in the popup window.

(because it was only 4-5 files i renamed the files first) I had to do it twice as some items was not deleted on the first try...

Yuli Hua
  • 561
  • 1
  • 4
  • 10