Table of Contents

Adding .gitignore

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. .

1️⃣ Before committing anything (best practice)

If you haven’t committed yet:

  1. In your project folder, create a .gitignore file:
touch .gitignore
  1. Edit it and add patterns for files/folders you want Git to ignore, e.g.:
# Python
__pycache__/
*.pyc

# VSCode
.vscode/

# Data
data/
  1. Then stage and commit:
git add .gitignore
git commit -m "Add .gitignore"
git push

✅ This way, Git never tracks the ignored files .

2️⃣ After some commits

If you already committed files that should be ignored:

  1. Add the .gitignore file and commit it.
  2. Remove the already-tracked files from Git (without deleting them locally):
git rm --cached filename_or_folder
git commit -m "Stop tracking files now in .gitignore"

Key points

💡 .gitignore Template

Here’s a minimal .gitignore template for Python/R projects, focused on the files you usually don’t want in Git:

snippet.gitignore
# 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/

✅ Usage

  1. Save it as .gitignore in your project root.
  2. Stage and commit it:
git add .gitignore
git commit -m "Add minimal .gitignore template for Python/R"
git push

💡 If you already tracked files that should now be ignored:

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/.