Version control is essential for modern software development. Whether you're a solo developer or part of a large team, understanding Git and GitHub is crucial. This comprehensive guide will take you from complete beginner to confident user.
Git is a distributed version control system that tracks changes in your code over time. Think of it as a time machine for your project that allows you to:
GitHub is a web-based platform that hosts Git repositories. It adds collaboration features and tools on top of Git:
Windows:
# Download from https://git-scm.com/download/win
macOS:
brew install git
Linux:
sudo apt-get install git # Ubuntu/Debian
sudo yum install git # CentOS/RHEL
After installation, set up your identity:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"# Initialize a new repository
git init
# Clone an existing repository
git clone https://github.com/username/repository.git# Check status of your repository
git status
# Add files to staging area
git add filename
git add . # Add all files
# Commit changes
git commit -m "Your commit message"
# Push changes to remote repository
git push origin main
# Pull changes from remote repository
git pull origin main# Create a new file
echo "Hello, World!" > hello.txt
# Check status
git status
# Add the file
git add hello.txt
# Commit the change
git commit -m "Add hello.txt file"
# Push to remote repository
git push origin mainBranches allow you to work on features independently.
# Create new branch
git branch feature-name
# Switch to branch
git checkout feature-name
# Create and switch in one command
git checkout -b feature-name
# List all branches
git branch -a
# Merge branch into current branch
git merge feature-name
# Delete branch
git branch -d feature-name# Create feature branch
git checkout -b new-feature
# Make changes and commit
echo "New feature code" > feature.txt
git add feature.txt
git commit -m "Add new feature"
# Switch back to main branch
git checkout main
# Merge feature branch
git merge new-featurePull requests (PRs) are GitHub's way to propose changes to a repository.
Example workflow:
# Fork repository on GitHub
# Clone your fork
git clone https://github.com/your-username/repository.git
# Create branch
git checkout -b feature-branch
# Make changes and commit
git add .
git commit -m "Add new feature"
# Push to your fork
git push origin feature-branch
# Create pull request on GitHub# Stash current changes
git stash
# List stashes
git stash list
# Apply stashed changes
git stash apply
# Remove stash
git stash drop# Rebase current branch onto main
git checkout feature-branch
git rebase main
# Interactive rebase
git rebase -i HEAD~3# Create tag
git tag v1.0.0
# Create annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"
# Push tags
git push origin --tagsUse imperative mood:
# Good
Add user authentication
# Bad
Added user authentication
Keep messages clear and concise
Include context when necessary
# When conflict occurs:
1. Open conflicted files
2. Look for conflict markers (<<<<<<, =======, >>>>>>>)
3. Resolve conflicts
4. Add resolved files
5. Commit changes# Undo last commit (keep changes)
git reset HEAD~1
# Revert to specific commit
git revert commit-hash
# Force reset to commit (discard changes)
git reset --hard commit-hash| Command | Description |
|---|---|
git init | Initialize repository |
git clone | Clone repository |
git add | Stage changes |
git commit | Commit changes |
git push | Push to remote |
git pull | Pull from remote |
git branch | Manage branches |
git checkout | Switch branches |
git merge | Merge branches |
git status | Check status |
Git and GitHub are powerful tools that become more valuable as you use them. Start with the basics, practice regularly, and gradually incorporate more advanced features into your workflow. Remember:
Q: What's the difference between Git and GitHub? A: Git is the version control system, while GitHub is a platform that hosts Git repositories and adds collaboration features.
Q: How do I undo a commit? A: Use git reset HEAD~1 to undo the last commit while keeping changes, or git reset --hard HEAD~1 to discard changes.
Q: What's a pull request? A: A pull request is a way to propose changes to a repository. It allows for code review and discussion before merging.
Start your Git journey today and join the millions of developers using version control to build better software!