Git is an essential tool for software development, whether for small or large projects. 🛠️ Proper usage of Git enhances teamwork efficiency, reduces code conflicts, and allows easy rollback to previous states. 🏗️
Before using Git, configure your user details:
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
Check current configuration:
git config --list
- Use clear and concise descriptions
- Start with a verb indicating the action, such as
Add
,Fix
,Update
- Reference related issues or features, e.g.,
Fixes #42
git commit -m "Fix: Resolve login page loading issue (Fixes #42)" # Fixed login page loading issue (Fixes #42)
git commit -m "Feat: Add Dark Mode to UI" # Added Dark Mode feature to UI
git commit -m "Docs: Update README with API usage instructions" # Updated README with API usage details
git commit -m "Perf: Improve Dashboard loading performance" # Optimized Dashboard loading speed
git commit -m "Refactor: Restructure authentication system code" # Refactored authentication system code structure
If you accidentally delete a file and haven't committed yet, restore it:
git checkout -- filename
If the file was already committed and then deleted:
git revert HEAD -- filename
If you need to edit the last commit message or add files:
git commit --amend -m "Fix: Improve validateUser function code" # Improved validateUser function code
To undo all uncommitted changes:
git reset --hard HEAD
To keep changes but remove the last commit:
git reset --soft HEAD~1
If a conflict occurs during a merge:
- Open the conflicted file and manually resolve the changes.
- Stage the resolved file:
git add resolved-file.js
- Commit the merge resolution:
git commit -m "Fix merge conflict in resolved-file.js" # Resolved merge conflict in resolved-file.js
To switch to a previous version while keeping changes:
git checkout commit-hash
To fully revert to a previous commit and discard changes:
git reset --hard commit-hash
- 🛑 Use .gitignore to exclude unnecessary files from commits.
- ⚡ Set up Git Aliases to shorten commands:
git config --global alias.co checkout git config --global alias.cm "commit -m"
- 📊 View a Clean Log History
git log --oneline --graph --decorate --all
- 🔄 Use Git Hooks such as
pre-commit
for automatic checks before committing.
✅ Configure Git properly
✅ Create and manage branches efficiently
✅ Commit and push code systematically
✅ Keep your code updated to avoid conflicts
✅ Use Git Stash and Rebase for a clean workflow
✅ Set up Aliases and Hooks to improve efficiency
✅ Troubleshoot common Git issues like file recovery, conflicts, and resets
Proper Git usage ensures a smooth development process and enhances collaboration! 🚀