0

By following this question (How to make Git “forget” about a file that was tracked but is now in .gitignore?) I was able to remove any node_modules that were included in the project from the index of git.

Although it is not tracked by git in my branch, it deletes the node_modules when someone pulls my branch.

Is there a possible solution to avoid this.

1615903
  • 32,635
  • 12
  • 70
  • 99
Harish
  • 490
  • 2
  • 11
  • 19
  • If your branch deletes the directory, make it not do that. – tripleee Nov 20 '17 at 11:27
  • The folder still shows in my local – Harish Nov 20 '17 at 11:31
  • If you deleted it, then added it to `.gitignore`, then added it locally without committing it, that's what happens. The commit which deletes the directory is still there, and others who merge that commit will lose the directory (or more likely get a conflict). – tripleee Nov 20 '17 at 11:32
  • I have only removed from cache using `git rm --cached` – Harish Nov 20 '17 at 11:35
  • You should probably show a [mcve] to indicate *exactly* how to repro this particular problem. – tripleee Nov 20 '17 at 11:38
  • To reproduce the problem, follow these steps 1. Install something in node_modules folder without adding the folder to .gitignore. 2. Remove the installed folders using git rm --cached -r node_modules 3. commit the changes 4. Add the node_modules folder to .gitignore 5. commit again 6. push changes and pull the changes to another branch where the node_modules folder is not ignored. The files get deleted on pull. – Harish Nov 20 '17 at 11:46

1 Answers1

1

Is there a possible solution to avoid this.

There is a way explained here (using .idea folder as an example):

If the other developers run git rm -r --cached .idea and commit that to their local trees before pulling your changes, Git will see that these two changes are equivalent (both "delete" the folder) and thus not try to delete it again.

And also a workaround:

If the folder gets deleted on their machine, they can simply restore it again. After all, this is version control!

There is the git checkout command that asks Git to place any file from any point in time in your current working copy. So if the last commit that your coworker pulled from you caused the .idea folder to be deleted, she could just say git checkout HEAD^ .idea and get it back from the version before. If there is more than one commit in between, first find the commit where the file has been deleted (git log --stat --diff-filter=D might be handy) and then use the commit before. So, for example, if the folder was deleted in commit c4f3d00d, use git checkout caf3d00d~1 .idea.

1615903
  • 32,635
  • 12
  • 70
  • 99