
Git & GitHub for Beginners
Git & GitHub for Beginners: Commands, SSH, Private vs Public Repos & Workflow
Git is the most popular version control system that tracks changes in your code, while GitHub is a cloud platform for hosting Git repositories. Together, they’re essential tools for every developer, enabling collaboration, backup, and project management.
In this comprehensive guide, you’ll learn:
- What Git and GitHub are and why they matter
- Essential Git commands and workflows
- Private vs Public repositories
- SSH setup and authentication
- Cloning, branching, and collaboration
- Best practices and real-world examples
- Free learning resources
What Are Git and GitHub?
Git (Version Control System)
Git tracks changes in your files over time, allowing you to:
- Save snapshots of your project (commits)
- Revert to previous versions
- Work on different features simultaneously (branches)
- Collaborate with others without conflicts
GitHub (Cloud Platform)
GitHub hosts your Git repositories online, providing:
- Remote backup of your code
- Collaboration tools (pull requests, issues)
- Project management features
- Portfolio showcase for developers
# Think of it this way:
# Git = Local version control on your computer
# GitHub = Online storage + collaboration platform
Installing Git
Windows
# Download from: https://git-scm.com/download/win
# Or use package manager
winget install Git.Git
macOS
# Using Homebrew
brew install git
# Or download from: https://git-scm.com/download/mac
Linux (Ubuntu/Debian)
sudo apt update
sudo apt install git
Verify Installation:
git --version
# Output: git version 2.42.0
Initial Git Setup
Configure your identity (required for commits):
# Set your name and email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Check your configuration
git config --list
Essential Git Commands
1. Repository Initialization
# Create a new Git repository
git init
# Check repository status
git status
# Example output:
# On branch main
# No commits yet
# nothing to commit (create/copy files and use "git add" to track)
2. Staging and Committing
# Add files to staging area
git add filename.txt # Add specific file
git add . # Add all files
git add *.js # Add all JavaScript files
# Check what's staged
git status
# Commit changes
git commit -m "Add initial project files"
# Add and commit in one step
git commit -am "Update existing files"
3. Viewing History
# View commit history
git log
# Compact view
git log --oneline
# View changes
git diff # Unstaged changes
git diff --staged # Staged changes
git diff HEAD~1 # Compare with previous commit
4. Branching
# List branches
git branch
# Create new branch
git branch feature-login
# Switch to branch
git checkout feature-login
# Create and switch in one command
git checkout -b feature-signup
# Modern way (Git 2.23+)
git switch feature-login
git switch -c feature-signup
# Delete branch
git branch -d feature-login
5. Merging
# Switch to main branch
git checkout main
# Merge feature branch
git merge feature-login
# Delete merged branch
git branch -d feature-login
Remote Repositories (GitHub)
Connecting to GitHub
# Add remote repository
git remote add origin https://github.com/username/repository.git
# View remotes
git remote -v
# Push to GitHub
git push -u origin main # First time
git push # Subsequent pushes
# Pull from GitHub
git pull origin main
Cloning Repositories
# Clone with HTTPS
git clone https://github.com/username/repository.git
# Clone with SSH (after SSH setup)
git clone git@github.com:username/repository.git
# Clone to specific folder
git clone https://github.com/username/repository.git my-project
# Clone specific branch
git clone -b develop https://github.com/username/repository.git
Private vs Public Repositories
Public Repositories
Characteristics:
- ✅ Visible to everyone on the internet
- ✅ Anyone can view and clone
- ✅ Great for open-source projects
- ✅ Free on GitHub
- ✅ Good for portfolio/showcase
Example Use Cases:
# Portfolio website
git clone https://github.com/username/portfolio.git
# Open source library
git clone https://github.com/facebook/react.git
# Learning projects
git clone https://github.com/username/javascript-projects.git
Private Repositories
Characteristics:
- 🔒 Only visible to you and collaborators
- 🔒 Requires authentication to access
- 🔒 Perfect for commercial projects
- 🔒 Free on GitHub (with limits)
- 🔒 Secure for sensitive code
Example Use Cases:
# Company project (requires authentication)
git clone https://github.com/company/secret-project.git
# Personal projects with sensitive data
git clone git@github.com:username/private-app.git
# Client work
git clone https://github.com/username/client-website.git
Comparison Table
Feature | Public Repository | Private Repository |
---|---|---|
Visibility | Everyone | Invited users only |
Cost | Free | Free (limited) |
Cloning | Anyone | Authenticated users |
Search | Appears in search | Hidden |
Forks | Anyone can fork | Collaborators only |
Issues | Public discussions | Private discussions |
Best For | Open source, portfolios | Commercial, sensitive |
SSH Setup for GitHub
SSH provides secure, password-free authentication with GitHub.
1. Generate SSH Key
# Generate new SSH key
ssh-keygen -t ed25519 -C "your.email@example.com"
# For older systems
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
# Press Enter to accept default location
# Enter passphrase (optional but recommended)
2. Add SSH Key to SSH Agent
# Start SSH agent
eval "$(ssh-agent -s)"
# Add SSH key to agent
ssh-add ~/.ssh/id_ed25519
# On macOS, add to keychain
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
3. Add SSH Key to GitHub
# Copy public key to clipboard
# On macOS
pbcopy < ~/.ssh/id_ed25519.pub
# On Linux
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
# On Windows
clip < ~/.ssh/id_ed25519.pub
Then on GitHub:
- Go to Settings → SSH and GPG keys
- Click “New SSH key”
- Paste your public key
- Give it a descriptive title
4. Test SSH Connection
# Test connection
ssh -T git@github.com
# Expected output:
# Hi username! You've successfully authenticated, but GitHub does not provide shell access.
5. Use SSH URLs
# Clone with SSH
git clone git@github.com:username/repository.git
# Change existing repo to SSH
git remote set-url origin git@github.com:username/repository.git
# Clone with specific SSH key (useful when you have multiple keys)
GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa_[SPECIFIC]' git clone git@github.com:username/repo.git
Complete Git Workflow Examples
Starting a New Project
# 1. Create project folder
mkdir my-awesome-project
cd my-awesome-project
# 2. Initialize Git
git init
# 3. Create initial files
echo "# My Awesome Project" > README.md
echo "console.log('Hello World!');" > app.js
# 4. Add and commit
git add .
git commit -m "Initial commit: Add README and app.js"
# 5. Create GitHub repository (on GitHub.com)
# Then connect local repo to GitHub
git remote add origin git@github.com:username/my-awesome-project.git
git push -u origin main
Working with Existing Project
# 1. Clone repository
git clone git@github.com:username/existing-project.git
cd existing-project
# 2. Create feature branch
git checkout -b feature/user-authentication
# 3. Make changes
echo "// User login functionality" >> auth.js
# 4. Stage and commit
git add auth.js
git commit -m "Add user authentication module"
# 5. Push feature branch
git push -u origin feature/user-authentication
# 6. Create Pull Request on GitHub
# 7. After review, merge and cleanup
git checkout main
git pull origin main
git branch -d feature/user-authentication
Collaborative Workflow
# 1. Always start with latest code
git checkout main
git pull origin main
# 2. Create feature branch
git checkout -b feature/shopping-cart
# 3. Work on feature
# ... make changes ...
git add .
git commit -m "Implement shopping cart functionality"
# 4. Push and create PR
git push -u origin feature/shopping-cart
# Create Pull Request on GitHub
# 5. Handle feedback
# ... make more changes ...
git add .
git commit -m "Address PR feedback: improve error handling"
git push
# 6. After merge, cleanup
git checkout main
git pull origin main
git branch -d feature/shopping-cart
git push origin --delete feature/shopping-cart
Advanced Git Commands
Undoing Changes
# Unstage files
git reset filename.txt
# Discard working directory changes
git checkout -- filename.txt
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert a commit (safe for shared repos)
git revert HEAD
Stashing Changes
# Save work in progress
git stash
# List stashes
git stash list
# Apply latest stash
git stash pop
# Apply specific stash
git stash apply stash@{0}
# Stash with message
git stash push -m "Work in progress on login feature"
Viewing and Comparing
# Show commit details
git show HEAD
git show abc123
# Compare branches
git diff main..feature-branch
# Show file history
git log --follow filename.txt
# Show who changed what
git blame filename.txt
GitHub Features
Issues and Project Management
# Reference issues in commits
git commit -m "Fix login bug (closes #15)"
# Link to issues
git commit -m "Implement user profile (ref #23)"
Pull Requests
Creating Pull Requests:
- Push feature branch to GitHub
- Go to repository on GitHub
- Click “Compare & pull request”
- Add description and reviewers
- Create pull request
Pull Request Best Practices:
- Write clear titles and descriptions
- Reference related issues
- Add screenshots for UI changes
- Request specific reviewers
- Keep PRs small and focused
GitHub Actions (CI/CD)
# .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm install
- run: npm test
Common Git Scenarios
Merge Conflicts
# When merge conflicts occur
git merge feature-branch
# Auto-merging index.html
# CONFLICT (content): Merge conflict in index.html
# Edit conflicted files, look for:
<<<<<<< HEAD
Current branch content
=======
Feature branch content
>>>>>>> feature-branch
# After resolving conflicts
git add .
git commit -m "Resolve merge conflicts"
Working with Forks
# Fork repository on GitHub, then clone your fork
git clone git@github.com:yourusername/forked-repo.git
cd forked-repo
# Add original repository as upstream
git remote add upstream git@github.com:original-owner/original-repo.git
# Keep your fork updated
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
Large Files with Git LFS
# Install Git LFS
git lfs install
# Track large files
git lfs track "*.psd"
git lfs track "*.zip"
# Add .gitattributes
git add .gitattributes
# Normal git workflow
git add large-file.psd
git commit -m "Add design files"
git push
Git Best Practices
Commit Messages
# Good commit messages
git commit -m "Add user authentication system"
git commit -m "Fix: Resolve login redirect issue"
git commit -m "Update: Improve error handling in API calls"
# Bad commit messages
git commit -m "fix"
git commit -m "changes"
git commit -m "stuff"
Branching Strategy
# Feature branches
git checkout -b feature/user-profile
git checkout -b feature/payment-integration
# Bug fixes
git checkout -b bugfix/login-error
git checkout -b hotfix/security-patch
# Releases
git checkout -b release/v1.2.0
.gitignore File
# Create .gitignore file
cat > .gitignore << EOF
# Dependencies
node_modules/
vendor/
# Environment files
.env
.env.local
# Build outputs
dist/
build/
*.log
# IDE files
.vscode/
.idea/
*.swp
# OS files
.DS_Store
Thumbs.db
EOF
Free Git & GitHub Learning Resources
Interactive Tutorials
- 🔗 GitHub Skills – Interactive GitHub courses
- 🔗 Learn Git Branching – Visual Git tutorial
- 🔗 Git Immersion – Hands-on Git workshop
- 🔗 Atlassian Git Tutorials – Comprehensive guides
Documentation & References
- 📚 Pro Git Book – Free official Git book
- 📚 GitHub Docs – Official GitHub documentation
- 📚 Git Reference – Command reference
Video Courses
- 🎥 Git & GitHub Crash Course – Traversy Media
- 🎥 Git Tutorial for Beginners – Programming with Mosh
- 🎥 GitHub for Beginners – freeCodeCamp
Practice Platforms
- 💻 GitHub – Create free account and practice
- 💻 GitLab – Alternative platform with CI/CD
- 💻 Bitbucket – Atlassian’s Git platform
Troubleshooting Common Issues
Authentication Problems
# Update remote URL to use SSH
git remote set-url origin git@github.com:username/repo.git
# Clear credential cache
git config --global --unset credential.helper
# Re-authenticate with GitHub CLI
gh auth login
Merge Conflicts
# Abort merge if needed
git merge --abort
# Use merge tool
git mergetool
# Configure merge tool
git config --global merge.tool vimdiff
Accidental Commits
# Undo last commit but keep changes
git reset --soft HEAD~1
# Undo and discard changes
git reset --hard HEAD~1
# Amend last commit message
git commit --amend -m "New commit message"
GitHub Alternatives
GitLab
- Self-hosted or cloud
- Built-in CI/CD
- Free private repositories
Bitbucket
- Atlassian ecosystem
- Free for small teams
- Integrated with Jira
SourceForge
- Open source focus
- Free hosting
- Long-established platform
Final Thoughts
Git and GitHub are essential tools for modern software development. Start with basic commands like add
, commit
, and push
, then gradually learn advanced features like branching, merging, and collaboration workflows.
Pro Tip: Practice with personal projects first. Create a repository for your learning exercises, portfolio, or side projects. The more you use Git daily, the more natural it becomes.
Remember that Git is a powerful tool with many features. Don’t try to learn everything at once. Focus on the core workflow first, then expand your knowledge as you encounter new scenarios.
Next Steps:
- Create your first GitHub repository
- Set up SSH authentication
- Practice the basic Git workflow
- Contribute to an open-source project
- Explore GitHub Actions for automation
Git and GitHub will become second nature with consistent practice. They’re not just tools—they’re essential skills that will serve you throughout your development career!