Quick tips, reminders, and useful patterns to help you become more efficient with Git.
- Commit often, but with purpose. Small, meaningful commits make it easier to track changes.
- Write clear commit messages. Use the imperative mood and describe what and why.
- Tidy up before sharing. Use
git rebase -ito squash, reword, or drop commits before pushing a feature branch. - Avoid committing secrets. Use
.gitignoreto exclude sensitive files like.env. - Pull before pushing. Always sync your branch before pushing changes to avoid conflicts.
- Use branches wisely. Keep
mainormasterclean; do experimental work in feature branches.
-
Undo last commit (keep changes):
git reset --soft HEAD~1
-
Undo last commit (discard changes):
git reset --hard HEAD~1
-
Amend the previous commit:
git commit --amend
-
Temporarily save uncommitted work:
git stash git stash pop
-
View changes before committing:
git diff
-
View branch history visually:
git log --oneline --graph --decorate --all
-
Create a
devbranch for testing changes before merging tomain. -
Use Pull Requests (PRs) even in solo projects for practice and documentation.
-
Force push safely: If you rebase or amend a shared branch, use git
push --force-with-leaseinstead of--forceto prevent overwriting someone else's work. -
Tag versions using semantic versioning:
git tag -a v1.0.0 -m "Initial release"
-
Clean up merged branches (local):
git branch --merged | grep -v "main" | xargs git branch -d
-
Prune remote-tracking branches:
git remote prune origin
Use GitHub Desktop or GitKraken for visualizing commits when learning Git workflows.
-
Start with a template: Use gitignore.io to generate
.gitignorefor your tech stack. -
Common patterns:
# Dependencies node_modules/ vendor/ # Environment variables .env .env.local # IDE and editor files .vscode/ .idea/ *.swp *~ # OS files .DS_Store Thumbs.db # Build outputs dist/ build/ *.log
-
Don't commit sensitive data: API keys, passwords, tokens should never be in version control.
-
Global gitignore: Create
~/.gitignore_globalfor personal files:git config --global core.excludesfile ~/.gitignore_global
Create a commit message template for consistency:
1. Create template file ~/.gitmessage:
# <type>(<scope>): <subject>
# |<---- Using a Maximum Of 50 Characters ---->|
# Explain why this change is being made
# |<---- Try To Limit Each Line to a Maximum Of 72 Characters ---->|
# Provide links or keys to any relevant tickets, articles or other resources
# Example: closes #23
# --- COMMIT END ---
# Type can be
# feat (new feature)
# fix (bug fix)
# refactor (refactoring code)
# style (formatting, missing semi colons, etc)
# docs (changes to documentation)
# test (adding or refactoring tests)
# chore (maintain)
# --------------------
# Remember to:
# - Capitalize the subject line
# - Use the imperative mood in the subject line
# - Do not end the subject line with a period
# - Separate subject from body with a blank line
# - Use the body to explain what and why vs. how
# - Can use multiple lines with "-" or "*" for bullet points in body
2. Configure Git to use it:
git config --global commit.template ~/.gitmessage- Make hooks executable:
chmod +x .git/hooks/pre-commit - Share hooks with team: Use tools like Husky (Node.js) or pre-commit (Python)
- Common use cases:
pre-commit: Run linters, formatters, testscommit-msg: Enforce commit message formatpre-push: Run full test suite
Example pre-commit hook (lint check):
#!/bin/sh
npm run lint
if [ $? -ne 0 ]; then
echo "❌ Linting failed. Fix errors before committing."
exit 1
fi- Keep PRs small: Easier to review, less likely to introduce bugs
- Self-review first: Review your own changes before requesting review
- Write descriptive PR descriptions: Include what, why, and how
- Use draft PRs: For work-in-progress that needs early feedback
- Respond to feedback: Address comments or explain why you disagree
- Use conventional comments:
nit:- Minor suggestion, not blockingquestion:- Asking for clarificationsuggestion:- Proposing an alternativeblocking:- Must be addressed before merge
-
Delete merged branches: Keep repository clean
git branch -d feature/completed-feature git push origin --delete feature/completed-feature
-
Run garbage collection periodically:
git gc --aggressive --prune=now
-
Keep commits atomic: One logical change per commit
-
Squash fixup commits: Before merging to main
git rebase -i main
-
Use meaningful branch names:
feat/user-auth, notmy-branch
Explore related Git documentation:
- 📘 Basics — Fundamental Git commands and usage
- 🧩 Prefixes — Commit and branch naming conventions
- ⚙️ Advanced — Advanced Git commands and workflows
- 🚀 Modern — Modern Git features and tools
- 🔧 Troubleshooting — Common problems and solutions
- ⚙️ Config — Useful aliases and configuration examples
- 📋 Cheat Sheet — Quick command reference
- 🔗 Resources — External references and learning materials
These tips help you maintain clean, efficient, and professional Git workflows every day.