Git Ignore, Stash and Tags
These tools keep your repository clean and make everyday work smoother.
.gitignore
Some files should never go into Git:
- Passwords and API keys (like
.envfiles) - Downloaded packages, such as
node_modules/orvenv/ - Build output, such as
dist/orbuild/ - System files like
.DS_StoreorThumbs.db - Editor settings and log files
Create a file named .gitignore in the main folder of your project:
Example
# Secrets
.env
# Dependencies
node_modules/
venv/
# Build output
dist/
build/
# Logs and system files
*.log
.DS_Store
Thumbs.db| Pattern | Ignores |
|---|---|
.env | A file named .env in any folder |
node_modules/ | A whole folder |
*.log | Every file ending in .log |
/config.php | Only config.php in the main folder |
!keep.log | An exception: do not ignore this file |
Commit .gitignore itself, so everyone on the team uses the same rules.
.gitignore only works for files Git is not tracking yet. If you already committed .env, remove it from Git with git rm --cached .env, commit, and change the password or key, because it is still in the history.
GitHub offers ready-made .gitignore templates for Node, Python, PHP and more when you create a repository.
git stash
You are halfway through a feature when an urgent bug comes in. Your work is not ready to commit. Stash it:
Example
git stashSaved working directory and index state WIP on main: 8b41d07 Add basic stylesheet
Your files are back to the last commit. Fix the bug, commit it, then bring your work back:
Example
git stash popOn branch main
Changes not staged for commit:
modified: index.html
Dropped refs/stash@{0} (1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b)| Command | What it does |
|---|---|
git stash | Save and hide uncommitted changes |
git stash -u | Also stash new, untracked files |
git stash list | Show all stashes |
git stash pop | Bring back the latest stash and remove it from the list |
git stash apply | Bring it back but keep it in the list |
git stash drop | Delete the latest stash |
Tags
A tag gives a permanent name to an important commit, usually a release version:
Example
git tag -a v1.0 -m "First public version of the bakery site"
git tag
git push origin v1.0v1.0
Tags are not pushed automatically, so push them by name (or with git push --tags). On GitHub, you can turn a tag into a Release with notes and downloadable files.
Versions often follow semantic versioning: v1.4.2 means major 1, minor 4, patch 2.
Useful Aliases
Save typing with short names for long commands:
Example
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"
git stOn branch main nothing to commit, working tree clean
Good Git Habits
- Commit often, in small steps, each with a clear message.
- Run
git statusandgit diff --stagedbefore committing. - Never commit passwords, keys or
.envfiles. - Pull before you start work, and push at the end of the day.
- Use a branch for every feature or fix, not
main. - Do not rewrite history that others already have (no
resetor--amendafter pushing).
Exercise
Exercise
Which line in .gitignore stops every .log file from being tracked?
