Git Tutorial menu

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 .env files)
  • Downloaded packages, such as node_modules/ or venv/
  • Build output, such as dist/ or build/
  • System files like .DS_Store or Thumbs.db
  • Editor settings and log files

Create a file named .gitignore in the main folder of your project:

Example

text
# Secrets
.env

# Dependencies
node_modules/
venv/

# Build output
dist/
build/

# Logs and system files
*.log
.DS_Store
Thumbs.db
PatternIgnores
.envA file named .env in any folder
node_modules/A whole folder
*.logEvery file ending in .log
/config.phpOnly config.php in the main folder
!keep.logAn 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

bash
git stash
Output
Saved 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

bash
git stash pop
Output
On branch main
Changes not staged for commit:
	modified:   index.html
Dropped refs/stash@{0} (1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b)
CommandWhat it does
git stashSave and hide uncommitted changes
git stash -uAlso stash new, untracked files
git stash listShow all stashes
git stash popBring back the latest stash and remove it from the list
git stash applyBring it back but keep it in the list
git stash dropDelete the latest stash

Tags

A tag gives a permanent name to an important commit, usually a release version:

Example

bash
git tag -a v1.0 -m "First public version of the bakery site"
git tag
git push origin v1.0
Output
v1.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

bash
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"
git st
Output
On branch main
nothing to commit, working tree clean

Good Git Habits

  1. Commit often, in small steps, each with a clear message.
  2. Run git status and git diff --staged before committing.
  3. Never commit passwords, keys or .env files.
  4. Pull before you start work, and push at the end of the day.
  5. Use a branch for every feature or fix, not main.
  6. Do not rewrite history that others already have (no reset or --amend after pushing).

Exercise

Exercise

Which line in .gitignore stops every .log file from being tracked?

Learn Git with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp