Skip to content

Instantly share code, notes, and snippets.

@akosasante
Last active June 1, 2023 15:27
Show Gist options
  • Save akosasante/66e3333d13a1858283270d4a35847c4e to your computer and use it in GitHub Desktop.
Save akosasante/66e3333d13a1858283270d4a35847c4e to your computer and use it in GitHub Desktop.
Removing files from git that you accidentally committed (eg: node_modules)

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.

Steps

  1. Create a .gitignore file in the git repo if you haven't already.
touch .gitignore
  1. Open up the .gitignore file and add the unwanted paths to the file:
node_modules
.DS_Store
.env
  1. 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
  1. Commit the git repository without the node modules folder
git commit -m "Removed unwanted tracked files and folders"
  1. Push the repository to Github
git push
  1. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment