1

So I try to change branches and I get:

error: The following untracked working tree files would be overwritten by checkout:
env/local/include
    # many more files
env/local/lib
Please move or remove them before you can switch branches.
Aborting

So I try git rm -r --cached . as suggested by stackoverflow.com/ignoring-a-directory-from-a-git-repo-after-its-been-added

and I get:

 fatal: pathspec 'env/' did not match any files

I've tried various other paths. Are there files in there or not? And if so, how can I have git ignore them?

Thanks

Zoe
  • 27,060
  • 21
  • 118
  • 148
KindOfGuy
  • 3,081
  • 5
  • 31
  • 47

1 Answers1

1

git rm -r --cached . would work for files that you have added to the index.
But if you haven't added env/, you could consider add env/ and stash it (as in this question).

Other elements to be aware of:

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The whole /env file is in `.gitignore`, which should mean it is not respected by the index right? I don't know why this is a problem if they are being ignored. Thanks for your response. – KindOfGuy Oct 08 '12 at 10:41
  • 1
    @KindOfGuy If they weren't already added in the index (before being registered in the .gitignore), then I confirm a git rm --cached won't work (because they are no longer in the index, ie "cached"). However, they could have been added and committed in the other branch you want to checkout to, in which case the versioned elements in the target branch you erase the local files in your present branch, hence the warning/error message. – VonC Oct 08 '12 at 10:44
  • Sorry, I don't understand that last part. I think that they have been added and committed in the other branch, but I don't want them tracked in this branch. But why can't I checkout? Surely the disparity between the two branches shouldn't matter, and I can untrack things from one branch that are in the other until I need to merge. – KindOfGuy Oct 08 '12 at 10:48
  • @KindOfGuy the last part explains that if you have private branch in your current branch, (files not versioned at all), git will refuse to overwrite them if files with the same names are versioned in the branch you want to checkout: you need to rm or move those private files (with mv or rm OS commands, not with git commands) before git will allow for the checkout to proceed. – VonC Oct 08 '12 at 11:02
  • why does a checkout involve overwriting anything? I thought a checkout was just a switch? I want to keep this /env untracked in the current branch, and go and work on the other branch without causing any changes. I have committed on this branch so what's left to change? I'm a bit confused. Thanks for explaining! – KindOfGuy Oct 08 '12 at 11:09
  • 1
    You don't have committed env/ on the current branch (before checkout), because you told me it is ignored). But you have committed before `env/` on the other branch: if you wish to checkout that other branch, that would result in (private) `env/` to be overridden with versioned `env/`: you need to remove private `env/` before doing the checkout. – VonC Oct 08 '12 at 11:21
  • Thanks for persevering with me. What I don't get is why checkout would override something on this branch X, with stuff on the other branch Y. I thought checkout was a switch, leaving both branches intact. If I wanted stuff in X to have what Y has, I would merge them. Isn't that what merge is for? – KindOfGuy Oct 08 '12 at 11:41
  • And now what do I do if I want to keep these files in this repo, but have them ignored and go to the other branch, where I will probably also ignore them (but would like the option of not doing so)? It seems I can't ignore files and then move to another branch unless I completely remove those files! – KindOfGuy Oct 08 '12 at 11:57
  • It seems like I can use the GIT_DIR/info/exclude file to ignore stuff in just one branch. But I still don't know why checkout would affect a different branch, acting more like what I thought merge does. Why doesn't it just switch? – KindOfGuy Oct 08 '12 at 11:59
  • 1
    @KindOfGuy I think the simpler solution would be to make a second clone (of the same repo) elsewhere, directly on the target branch. That way, you don't need to switch branches in the same working directory. You have both branches checked out in their own working directory. – VonC Oct 08 '12 at 12:33