public:add_gitignore
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:
- In your project folder, create a
.gitignorefile:
touch .gitignore
- Edit it and add patterns for files/folders you want Git to ignore, e.g.:
# Python __pycache__/ *.pyc # VSCode .vscode/ # Data data/
- 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:
- Add the
.gitignorefile and commit it. - 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"
--cachedtells Git to stop tracking them but keep them on disk.- Repeat for all files already tracked that you want ignored.
Key points
.gitignoreonly prevents untracked files from being added in the future.- Already tracked files must be removed with
git rm --cached. - You can always edit
.gitignorelater and commit changes.
💡 .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
- Save it as
.gitignorein your project root. - 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/.
public/add_gitignore.txt · Last modified: by admins_ceab
