---
title: 'Git & GitHub Mastery: A Developer''s Guide'
description: 'Git + GitHub mastery: branching strategies, merges, rebase, pull requests, and the workflows (trunk-based, GitFlow) that shipping teams actually use today.'
url: https://nerdleveltech.com/git-github-mastery-a-developers-guide
language: en
date: '2026-03-28'
category: tooling
tags:
  - git tutorial
  - github tutorial
  - version control
  - git commands
  - github guide
  - git gui
  - github actions
  - git branching
  - git merge
  - ci/cd pipeline
markdown_url: https://nerdleveltech.com/git-github-mastery-a-developers-guide.md
attribution: 'Source: NerdLevelTech — https://nerdleveltech.com/git-github-mastery-a-developers-guide'
terms: https://nerdleveltech.com/llms.txt
---

# Git & GitHub Mastery: A Developer's Guide

### TL;DR
This comprehensive Git and GitHub tutorial covers everything from basic version control concepts to advanced workflows. You'll learn essential Git commands, branching strategies, merging techniques, and how to use GitHub for collaboration. The guide includes practical examples, visual aids, and troubleshooting tips to help you master version control. Key topics include:

- Setting up Git on any operating system
- Core commands and workflows
- Branching strategies and conflict resolution
- GitHub collaboration with pull requests
- CI/CD pipelines with GitHub Actions
- GUI client comparisons
- Common errors and solutions
- Best practices for professional development

### Introduction to Version Control & Git

Version control is the backbone of modern software development, enabling developers to track changes, collaborate effectively, and maintain a history of their codebase. Git, created by Linus Torvalds in 2005, has become the de facto standard for version control systems, powering millions of projects worldwide.

#### Why Version Control Matters

Without version control, developers face numerous challenges:
- Difficulty tracking changes across multiple files
- Risk of losing work due to accidental deletions or overwrites
- Challenges in collaborating with team members
- No clear history of who changed what and why

Git solves these problems by providing:
- Distributed version control (every developer has a full copy of the repository)
- Branching and merging capabilities
- Change tracking at the line level
- Cryptographic integrity of the entire codebase

#### Core Git Concepts

Understanding these fundamental concepts is crucial:

1. **Repository (repo)**: A collection of files and their complete history
2. **Commit**: A snapshot of your repository at a specific point in time
3. **Branch**: A parallel version of the codebase
4. **Clone**: A local copy of a remote repository
5. **Pull**: Fetching changes from a remote repository
6. **Push**: Sending changes to a remote repository
7. **Merge**: Combining changes from different branches
8. **Fork**: A personal copy of someone else's project

#### GitHub: The Collaboration Platform

GitHub is a cloud-based hosting service for Git repositories that adds powerful collaboration features:
- Web-based interface for repository management
- Pull requests for code review
- Issue tracking
- Project management tools
- GitHub Actions for automation
- Social coding features

### Setting Up Git

#### Installation

##### Windows
1. Download the latest Git for Windows installer from [git-scm.com](https://git-scm.com/download/win)
2. Run the installer with default settings
3. Verify installation:
```bash
git --version
```

##### macOS
Using Homebrew:
```bash
brew install git
```
Or download from [git-scm.com](https://git-scm.com/download/mac)

##### Linux (Ubuntu/Debian)
```bash
sudo apt update
sudo apt install git
```

#### Configuration

After installation, configure your identity:
```bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global init.defaultBranch main
```

Set up line ending preferences:
```bash
# Windows
git config --global core.autocrlf true

# Linux/macOS
git config --global core.autocrlf input
```

### Core Git Commands

#### Initializing a Repository
```bash
mkdir my-project
cd my-project
git init
```

#### Cloning an Existing Repository
```bash
git clone https://github.com/username/repository.git
```

#### Checking Status
```bash
git status
```

#### Staging Changes
```bash
# Stage specific file
git add filename.ext

# Stage all changes
git add .

# Stage specific changes in a file (interactive)
git add -p
```

#### Committing Changes
```bash
# Basic commit
git commit -m "Your commit message"

# Add and commit in one command
git commit -am "Your commit message"

# Amend the previous commit
git commit --amend
```

#### Viewing History
```bash
# Basic log
git log

# Compact log
git log --oneline --graph

# Show changes in a specific file
git log -p filename.ext

# Show who changed what and when
git blame filename.ext
```

#### Comparing Changes
```bash
# Show unstaged changes
git diff

# Show staged changes
git diff --staged

# Compare with specific commit
git diff commit_hash
```

### Branching and Merging

#### Creating and Switching Branches

Since Git 2.23, `git switch` is the recommended command for changing branches (replacing the overloaded `git checkout`):

```bash
# Create new branch
git branch feature-branch

# Switch to branch (modern)
git switch feature-branch

# Create and switch in one command (modern)
git switch -c feature-branch

# Legacy equivalents (still work, but git switch is clearer)
# git checkout feature-branch
# git checkout -b feature-branch
```

#### Merging Branches
```bash
# Switch to target branch
git switch main

# Merge feature branch
git merge feature-branch
```

#### Resolving Merge Conflicts

When conflicts occur:
1. Open the conflicting files
2. Look for conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`)
3. Resolve the conflicts
4. Stage the resolved files
5. Commit the changes

```bash
# After resolving conflicts
git add resolved-file.ext
git commit -m "Resolve merge conflict"
```

### Advanced Merging Strategies

#### Rebasing
```bash
# Rebase current branch onto main
git switch feature-branch
git rebase main

# Interactive rebase (for rewriting history)
git rebase -i HEAD~3  # Last 3 commits
```

#### Cherry-Picking
```bash
# Apply a specific commit to current branch
git cherry-pick commit-hash
```

#### Stashing Changes
```bash
# Save uncommitted changes
git stash

# Apply stashed changes
git stash pop

# List stashes
git stash list

# Apply specific stash
git stash apply stash@{n}
```

### GitHub: Hosting Your Code

#### Creating a New Repository
1. Click the "+" icon in the top-right corner
2. Select "New repository"
3. Enter repository name and description
4. Choose public or private
5. Initialize with a README (optional)
6. Click "Create repository"

#### Pushing to GitHub
```bash
# Add remote
git remote add origin https://github.com/username/repository.git

# Push to main branch
git push -u origin main
```

#### Pulling Changes
```bash
# Fetch and merge changes
git pull origin main

# Fetch changes without merging
git fetch origin
```

### GitHub Pull Requests & Code Review

#### Creating a Pull Request
1. Push your branch to GitHub
2. Click "Compare & pull request"
3. Add a descriptive title and description
4. Request reviewers
5. Click "Create pull request"

#### Code Review Best Practices
- Keep pull requests small and focused
- Include clear descriptions and context
- Link related issues
- Use @mentions to request specific reviewers
- Address all feedback before merging

#### Reviewing Code
- Check for functionality and edge cases
- Verify code style and consistency
- Look for potential security issues
- Test the changes locally if necessary
- Provide constructive feedback

### GitHub Actions & CI/CD

#### Creating a Basic Workflow
Create `.github/workflows/ci.yml`:
```yaml
name: CI Pipeline

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v6

    - name: Set up Node.js
      uses: actions/setup-node@v6
      with:
        node-version: 22

    - name: Install dependencies
      run: npm ci

    - name: Run tests
      run: npm test
```

> **Version pinning:** Always use a specific major version tag (`@v6`) rather than `@master` or `@main`. This prevents unexpected breaking changes in your workflows.

#### Deployment Workflow
```yaml
name: Deploy to Production

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v6

    - name: Deploy to server
      uses: appleboy/ssh-action@v1.2.5
      with:
        host: ${{ secrets.SSH_HOST }}
        username: ${{ secrets.SSH_USERNAME }}
        key: ${{ secrets.SSH_KEY }}
        script: |
          cd /var/www/myapp
          git pull origin main
          npm ci --omit=dev
          pm2 restart myapp
```

### Git GUI Clients: A Comparison

#### GitHub Desktop
**Pros:**
- Simple and intuitive interface
- Great for beginners
- Built-in GitHub integration
- Cross-platform (Windows, macOS)

**Cons:**
- Limited advanced features
- Less customizable than other clients

#### Sourcetree
**Pros:**
- Free and powerful
- Visual representation of branches
- Interactive rebase support
- Git-flow integration

**Cons:**
- Can be overwhelming for beginners
- Occasional performance issues with large repos

#### GitKraken
**Pros:**
- Beautiful, intuitive interface
- Built-in merge conflict editor
- Git-flow and GitHub Flow support
- Cross-platform (Windows, macOS, Linux)

**Cons:**
- Free version has limited features
- Resource-intensive

#### VS Code Git Integration
**Pros:**
- Built into popular editor
- Lightweight and fast
- Extensible with extensions
- Inline diff viewing

**Cons:**
- Less visual than dedicated clients
- Some advanced features require extensions

### Troubleshooting Common Git Errors

#### Merge Conflicts
**Symptom:** `CONFLICT (content): Merge conflict in file.ext`
**Solution:**
1. Open the file and resolve conflicts
2. Stage the resolved file
3. Commit the changes

#### Detached HEAD
**Symptom:** `You are in 'detached HEAD' state`
**Solution:**
```bash
# Create a new branch to save changes
git switch -c new-branch-name
```

#### Authentication Failed
**Symptom:** `fatal: Authentication failed for 'https://github.com/...'`
**Solution:**
1. Update credentials in credential manager
2. Or use SSH instead of HTTPS
3. Or use a personal access token

#### Uncommitted Changes
**Symptom:** `error: Your local changes to the following files would be overwritten`
**Solution:**
```bash
# Stash changes
git stash

# Or discard changes (modern — Git 2.23+)
git restore .

# Legacy equivalent: git checkout -- .
```

#### Large File Storage (LFS)
**Symptom:** `error: File is too large`
**Solution:**
1. Install Git LFS
2. Track large files:
```bash
git lfs install
git lfs track "*.psd"
git add .gitattributes
```

### Git Best Practices

#### Commit Messages
- Use the imperative mood ("Add feature" not "Added feature")
- Keep the subject line under 50 characters
- Add a blank line between subject and body
- Reference issues or tickets
- Explain why, not just what

Example:
```
Add user authentication middleware

- Implement JWT token verification
- Add protected routes
- Handle token expiration

Fixes #123
```

#### Branching Strategy
**Git Flow:**
- `main` - Production code
- `develop` - Integration branch
- `feature/` - New features
- `release/` - Preparation for production
- `hotfix/` - Critical production fixes

**GitHub Flow (simpler alternative):**
- `main` - Always deployable
- Feature branches - Created from main
- Pull requests for all changes
- Deploy from main branch

#### .gitignore
Always include a `.gitignore` file to exclude:
- Build artifacts
- Dependencies
- Environment files
- Editor-specific files
- Operating system files

Example `.gitignore` for Node.js:
```
# Dependencies
node_modules/

# Environment variables
.env

# Build output
dist/
build/

# Logs
logs/
*.log

# Editor files
.vscode/
.idea/
*.swp
```

#### Code Review Checklist
- [ ] Code follows style guidelines
- [ ] Tests are included and pass
- [ ] Documentation is updated
- [ ] No commented-out code
- [ ] Error handling is appropriate
- [ ] Performance considerations
- [ ] Security considerations

### Resources & Further Learning

#### Official Documentation
- [Git Documentation](https://git-scm.com/doc)
- [GitHub Docs](https://docs.github.com)
- [GitHub Skills](https://skills.github.com) — hands-on interactive courses (replaced the now-defunct GitHub Learning Lab)

#### Interactive Learning
- [Learn Git Branching](https://learngitbranching.js.org) — visual, interactive tutorial for branching and rebasing
- [Atlassian Git Tutorial](https://www.atlassian.com/git/tutorials)

#### Books
- "Pro Git" by Scott Chacon and Ben Straub — [free online at git-scm.com/book](https://git-scm.com/book/en/v2), regularly updated and the definitive reference

#### Cheat Sheets
- [Git Cheat Sheet (GitHub)](https://training.github.com/downloads/github-git-cheat-sheet/)
- [Git Commands Cheat Sheet (Atlassian)](https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet)

#### Community
- [Stack Overflow Git Questions](https://stackoverflow.com/questions/tagged/git)
- [GitHub Community Discussions](https://github.com/orgs/community/discussions/)
- [r/git Subreddit](https://www.reddit.com/r/git/)

[^git-docs]: Git Documentation. https://git-scm.com/doc
