Introduction
Git is the industry-standard version control system that facilitates efficient collaboration, change tracking, and error recovery. This guide encompasses everything from basic commands to advanced workflows, enabling you to use Git like a pro.
Why Use Git?
Git offers critical advantages for developers and teams:
Version Control: Effortlessly track changes and revert to previous states.
Collaboration: Facilitate seamless teamwork through branching and merging.
Undo Mistakes: Restore changes or recover deleted files.
Distributed System: Work offline and synchronize changes later.
Industry Standard: Commonly used in open-source and professional projects.
Essential Commands
Repository Setup
Command | Description | Example |
---|---|---|
git init | Initialize a new repository | git init |
git clone [URL] | Clone an existing repository | git clone https://github.com/user/repo.git |
Basic Workflow
Command | Description | Example |
---|---|---|
git add [file] | Stage changes | git add index.html |
git commit -m "msg" | Commit staged changes | git commit -m "Update navbar" |
git status | Check repository status | git status |
git push | Push changes to remote | git push origin main |
git pull | Fetch and merge remote changes | git pull |
Branching & Merging
Command | Description | Example |
---|---|---|
git branch | List branches | git branch |
git checkout -b [branch] | Create and switch to a branch | git checkout -b feature/login |
git merge [branch] | Merge a branch | git merge feature/login |
History & Differences
Command | Description | Example |
---|---|---|
git log | View commit history | git log --oneline |
git diff [file] | Show unstaged changes | git diff styles.css |
Advanced Commands
Rewriting History
Command | Description | Example |
---|---|---|
git rebase | Reapply commits on another branch | git rebase main |
git cherry-pick [commit] | Apply a specific commit | git cherry-pick abc123 |
Debugging
Command | Description | Example |
---|---|---|
git bisect | Binary search for bugs | git bisect start |
git blame [file] | Identify line changes | git blame config.yml |
Stashing & Submodules
Command | Description | Example |
---|---|---|
git stash | Temporarily save changes | git stash push -m "WIP" |
git submodule | Manage nested repositories | git submodule update --init |
Key Flags
Commit & Reset
Flag | Command | Effect |
---|---|---|
--amend | git commit --amend | Modify the last commit |
--hard | git reset --hard HEAD~1 | Discard all changes since the last commit |
Log & Diff
Flag | Command | Effect |
---|---|---|
--oneline | git log --oneline | Compact commit history |
--graph | git log --graph | Visualize branch structure |
Merge & Pull
Flag | Command | Effect |
---|---|---|
--no-ff | git merge --no-ff | Force merge commit |
--rebase | git pull --rebase | Avoid merging commits during the pull |
Real-World Use Cases
1. Team Collaboration
Scenario: Multiple developers work on an e-commerce feature.
Workflow
Create branches: git checkout -b feature/payment-api
Merge via PRs after review.
Resolve conflicts with git mergetool.
2. Hotfix Deployment
Scenario: Critical bug in production.
Workflow:
Branch from main: git checkout -b hotfix/login-bug
Fix and test.
Merge to main and deploy: git merge --no-ff hotfix/login-bug
3. Clean Commit History
Scenario: Clean up messy commits before pushing.
Workflow:
Interactive rebase: git rebase -i HEAD~3
Squash or reword commits.
Pro Tips
Branch Naming Conventions
Use prefixes: feature/
, bugfix/
, hotfix/
Include ticket IDs: feature/JIRA-123-new-form
Keep it short: docs/update-readme
Efficiency Hacks
- Aliases: Save time with shortcuts:
git config --global alias.st status
git config --global alias.co checkout
- Hooks: Automate tasks (e.g., run tests pre-commit).
- gitignore: Exclude build files, logs, and environment configs.
Undoing Mistakes
- Unstaged files:
git reset HEAD [file]
- Revert a commit:
git revert abc123
- Recover deleted files:
git checkout HEAD -- [file]
Conclusion
Git mastery comes with practice. Start small, experiment with branches, and leverage advanced features as you grow. Happy coding! 🚀
Post a Comment