Introduction to Git and Version Control
A comprehensive guide to Git version control system, covering basic commands, branching strategies, and best practices.
Git is a distributed version control system that allows you to track changes in your codebase. It's an essential tool for any developer.
What is Version Control?
Version control systems help you:
- Track changes to your code
- Collaborate with others
- Revert to previous versions
- Manage different features simultaneously
Basic Git Commands
Initializing a Repository
git init
Staging Changes
git add .
git add filename.py
Committing Changes
git commit -m "Your commit message"
Checking Status
git status
git log
Branching
Branches allow you to work on different features simultaneously:
# Create a new branch
git branch feature-name
# Switch to a branch
git checkout feature-name
# Create and switch in one command
git checkout -b feature-name
# List all branches
git branch
Remote Repositories
Adding a Remote
git remote add origin https://github.com/user/repo.git
Pushing to Remote
git push origin main
Pulling from Remote
git pull origin main
Common Workflows
Feature Branch Workflow
- Create a feature branch
- Make changes and commit
- Push branch to remote
- Create a pull request
- Merge after review
Git Flow
main: Production-ready codedevelop: Integration branchfeature/*: New featuresrelease/*: Release preparationhotfix/*: Emergency fixes
Best Practices
- Write clear, descriptive commit messages
- Commit often with logical units of work
- Use branches for features and fixes
- Never commit directly to main/master
- Review code before merging
- Keep commits focused and atomic
Useful Commands
# See what changed
git diff
# Undo changes
git checkout -- filename
# View commit history
git log --oneline --graph
# Stash changes
git stash
git stash pop
Mastering Git is essential for professional development. Practice regularly and you'll become proficient quickly.