Sometimes we accidentally stage and commit a file or folder that we don't mean to. In order to revert this, we can revert that commit. But if we've already done a bunch of subsequent work and commits, this might be a bit painful. An alternative is to just tell git to remove that file/folder from it's cache, so that it's no longer tracking it.
- Create a .gitignore file in the git repo if you haven't already.
touch .gitignore
- Open up the
.gitignore
file and add the unwanted paths to the file:
node_modules
.DS_Store
.env
- Remove the unwanted files/folders from the git repository's cache (but not the actual file in your working directory)
git rm -r --cached node_modules
git rm -r --cached .DS_Store
git rm -r --cached .env
- Commit the git repository without the node modules folder
git commit -m "Removed unwanted tracked files and folders"
- Push the repository to Github
git push
- Don't forget to add the gitignore and commit it to the repo, if you haven't already
git add .gitignore
git commit -m "Added .gitignore file"
git push