When changing .gitignore, do all developers need to run `git rm --cached <path>`?

1.5k views Asked by At

If I add file that is already tracked by Git to my .gitignore, I can simply run

git rm --cached <path>

to apply the new rules. I push that to the server, and the server will no longer receive my changes. Will I have to tell everyone on my dev team that they need to run the git rm command to make sure nobody ever pushes changes to the new file?

1

There are 1 answers

1
jub0bs On BEST ANSWER

Will I have to tell everyone on my dev team that they need to run the git rm command to make sure nobody ever pushes changes to the new file?

Short answer

No. All you have to do is commit and push to remote.

Detailed answer

If you want to start ignoring a file that is already being tracked by Git, you indeed need to

  1. add an entry for it in your .gitignore, and
  2. run git rm --cached <path> in order to stop tracking the file (Git cannot ignore files that are currently being tracked).

Now, if you commit and push to remote, the file in question will no longer be tracked in the remote repo. Therefore, after your collaborators incorporate the latest changes from the remote into their own local repos (e.g. by pulling), the file in question will also no longer be tracked in their own repos. Therefore, they will not need to run

git rm --cached <path>

Moreover, because your .gitignore is part of the repository (i.e. it is being tracked by Git), your collaborators won't even need to add an entry for the file in question; the entry will already be there in the .gitignore, as part of your changes that they've incorporated into their own local repos.

Of course, that's under the assumption that nobody does a force push (which has the potential to obliterate your changes) after you've pushed...