You can add a .gitignore file anytime to stop tracking ignored files. This keeps your repository clean and lightweight, only tracking code and important configs, not heavy data or temporary files.
.
If you haven’t committed yet:
.gitignore file:touch .gitignore
# Python __pycache__/ *.pyc # VSCode .vscode/ # Data data/
git add .gitignore git commit -m "Add .gitignore" git push
✅ This way, Git never tracks the ignored files .
If you already committed files that should be ignored:
.gitignore file and commit it.git rm --cached filename_or_folder git commit -m "Stop tracking files now in .gitignore"
--cached tells Git to stop tracking them but keep them on disk..gitignore only prevents untracked files from being added in the future.git rm --cached..gitignore later and commit changes.
Here’s a minimal .gitignore template for Python/R projects, focused on the files you usually don’t want in Git:
# Python cache and compiled files __pycache__/ *.py[cod] *.pyo *.pyd # Virtual environments env/ venv/ *.env *.venv # Jupyter Notebook checkpoints .ipynb_checkpoints/ # Logs *.log # R .Rhistory .RData .Rproj.user/ # Data files (optional: ignore large datasets) data/ *.csv *.tsv *.h5 *.sqlite # IDE/editor settings .vscode/ .idea/
.gitignore in your project root.git add .gitignore git commit -m "Add minimal .gitignore template for Python/R" git push
For Git to ignore a file/folder in the future, you need to explicitly add it to your .gitignore file:
Add the file/folder to .gitignore:
echo "data/" >> .gitignore ... repeat for all files/folders to be inored git add .gitignore git commit -m "Add data folder to .gitignore"
Remove already-tracked files from Git:
git rm --cached -r data/ ... repeat for all files/folders to be inored git commit -m "Stop tracking existing data files"
Push changes:
git push
After this, Git will ignore all current and future files in data/.