Complete Git Guide | Resources by Shumbul Arifa

🚀 Complete Git Guide

Master Git with comprehensive commands, workflows, and best practices for modern development

📋 Table of Contents

🔧 Setup & Configuration

Initial Setup

Before using Git, configure your identity:

bash
# Set your name and email globally
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Set default branch name to 'main'
git config --global init.defaultBranch main

# Set your preferred editor
git config --global core.editor "code --wait"  # VS Code
git config --global core.editor "vim"          # Vim
git config --global core.editor "nano"         # Nano

Viewing Configuration

bash
# View all configurations
git config --list

# View specific configuration
git config user.name
git config user.email

# View configuration with location info
git config --list --show-origin

💡 Configuration Levels

  • --system: System-wide configuration (all users)
  • --global: User-specific configuration
  • --local: Repository-specific configuration (default)

📁 Repository Operations

Creating Repositories

bash
# Initialize a new repository in current directory
git init

# Initialize with specific branch name
git init --initial-branch=main

# Clone an existing repository
git clone <repository-url>

# Clone to a specific directory
git clone <repository-url> <directory-name>

# Clone a specific branch
git clone -b <branch-name> <repository-url>

# Clone with limited history (shallow clone)
git clone --depth 1 <repository-url>

Repository Information

bash
# Show remote repositories
git remote -v

# Show repository statistics
git count-objects -v

# Show repository size
du -sh .git/

🔄 Basic Workflow

Checking Status

bash
# Check repository status (detailed)
git status

# Check status in short format
git status -s

# Check status and show ignored files
git status --ignored

Status Indicators in Short Format:

  • ?? - Untracked files
  • A - Added (staged)
  • M - Modified
  • D - Deleted
  • R - Renamed
  • C - Copied

Adding Files (Staging)

bash
# Add specific file
git add <filename>

# Add multiple files
git add file1.txt file2.txt

# Add all files in current directory
git add .

# Add all files with specific extension
git add *.js

# Add files interactively
git add -i

# Add parts of a file (patch mode)
git add -p <filename>

# Add all tracked files (excluding untracked)
git add -u

Committing Changes

bash
# Commit staged changes with message
git commit -m "Your commit message"

# Commit all tracked files (skip staging)
git commit -am "Your commit message"

# Commit with detailed message
git commit -m "Brief summary" -m "Detailed explanation"

# Commit and open editor for message
git commit

# Amend the last commit
git commit --amend

# Amend without changing message
git commit --amend --no-edit

đŸŒŗ Branching & Merging

Branch Management

bash
# List all local branches
git branch

# List all branches including remote
git branch -a

# Create new branch
git branch <branch-name>

# Create and switch to new branch
git checkout -b <branch-name>

# Switch to existing branch
git checkout <branch-name>

# Switch to previous branch
git checkout -

# Rename current branch
git branch -m <new-branch-name>

# Delete merged branch
git branch -d <branch-name>

# Force delete branch (even if not merged)
git branch -D <branch-name>

Merging Strategies

bash
# Fast-forward merge (default when possible)
git merge <branch-name>

# Create merge commit even if fast-forward is possible
git merge --no-ff <branch-name>

# Squash merge (combine all commits into one)
git merge --squash <branch-name>

# Abort ongoing merge
git merge --abort

# Continue merge after resolving conflicts
git merge --continue

🔧 Handling Merge Conflicts

  1. Step 1: Git marks conflicted files
  2. Step 2: Edit files to resolve conflicts
  3. Step 3: Remove conflict markers (<<<<<<<, =======, >>>>>>>)
  4. Step 4: Stage resolved files: git add <filename>
  5. Step 5: Complete merge: git commit

🌐 Remote Operations

Remote Repository Management

bash
# List remote repositories
git remote -v

# Add remote repository
git remote add <remote-name> <repository-url>

# Remove remote
git remote remove <remote-name>

# Rename remote
git remote rename <old-name> <new-name>

# Change remote URL
git remote set-url <remote-name> <new-url>

# Show remote information
git remote show <remote-name>

Fetching and Pulling

bash
# Fetch from default remote
git fetch

# Fetch from specific remote
git fetch <remote-name>

# Fetch all remotes
git fetch --all

# Fetch and prune deleted branches
git fetch --prune

# Pull changes (fetch + merge)
git pull

# Pull with rebase instead of merge
git pull --rebase

# Pull and automatically stash/unstash changes
git pull --autostash

Pushing Changes

bash
# Push to default remote and branch
git push

# Push to specific remote and branch
git push <remote-name> <branch-name>

# Push and set upstream tracking
git push -u <remote-name> <branch-name>

# Push all branches
git push --all

# Push tags
git push --tags

âš ī¸ Dangerous Operations

bash
# Force push (dangerous - rewrites history)
git push --force

# Safer force push (fails if remote has new commits)
git push --force-with-lease

# Delete remote branch
git push <remote-name> --delete <branch-name>

📜 Viewing History

Basic Log Commands

bash
# View commit history
git log

# Compact one-line format
git log --oneline

# Show graphical representation
git log --graph

# Combine graph with oneline
git log --graph --oneline --all

# Show file statistics
git log --stat

# Show actual changes
git log --patch

# Limit number of commits
git log -n 5
git log -5

Advanced Log Filtering

bash
# Filter by author
git log --author="John Doe"

# Filter by date range
git log --since="2023-01-01"
git log --until="2023-12-31"
git log --since="2 weeks ago"

# Filter by commit message
git log --grep="bug fix"

# Filter by file
git log -- <filename>
git log --follow -- <filename>  # Follow renames

# Filter by content changes
git log -S "function_name"      # When function_name was added/removed
git log -G "regex_pattern"      # When pattern was added/removed

Comparing Changes

bash
# Show unstaged changes
git diff

# Show staged changes
git diff --staged
git diff --cached

# Compare specific commits
git diff <commit1> <commit2>

# Compare branches
git diff <branch1> <branch2>

# Show changes introduced by a commit
git show <commit-hash>

# Show file at specific commit
git show <commit>:<filename>

â†Šī¸ Undoing Changes

Working Directory Changes

bash
# Discard changes in specific file
git checkout -- <filename>

# Discard all changes in working directory
git checkout -- .

# Restore file from specific commit
git checkout <commit> -- <filename>

# Using newer restore command (Git 2.23+)
git restore <filename>
git restore --staged <filename>  # Unstage
git restore --source=<commit> <filename>

Cleaning Untracked Files

bash
# Remove untracked files
git clean -f

# Remove untracked files and directories
git clean -fd

# Preview what will be removed
git clean -n

# Interactive clean
git clean -i

# Remove ignored files too
git clean -fx

Commit History Changes

bash
# Undo last commit, keep changes staged
git reset --soft HEAD~1

# Undo last commit, keep changes in working directory
git reset --mixed HEAD~1
git reset HEAD~1  # default is --mixed

# Undo last commit, discard all changes
git reset --hard HEAD~1

# Reset to specific commit
git reset --hard <commit-hash>

# Create new commit that undoes changes
git revert <commit-hash>

# Revert merge commit
git revert -m 1 <merge-commit-hash>

đŸ“Ļ Stashing

Basic Stashing

bash
# Stash current changes
git stash
git stash push  # newer syntax

# Stash with message
git stash save "Work in progress on feature X"
git stash push -m "Work in progress on feature X"

# Stash including untracked files
git stash -u
git stash --include-untracked

# Stash only specific files
git stash push <filename>

Managing Stashes

bash
# List all stashes
git stash list

# Show stash content
git stash show
git stash show -p  # show patch

# Apply latest stash
git stash apply

# Apply specific stash
git stash apply stash@{2}

# Pop stash (apply and remove)
git stash pop
git stash pop stash@{1}

# Drop specific stash
git stash drop stash@{0}

# Clear all stashes
git stash clear

Advanced Stashing

bash
# Create branch from stash
git stash branch <branch-name> stash@{0}

# Stash only staged changes
git stash --staged

# Interactive stashing
git stash -p

🔄 Common Workflows

Feature Branch Workflow

  1. Update main: git checkout main && git pull origin main
  2. Create feature branch: git checkout -b feature/user-authentication
  3. Work on feature: Make changes, commit regularly
  4. Push feature: git push -u origin feature/user-authentication
  5. Create pull request: Via GitHub/GitLab interface
  6. After approval: Merge and clean up branches

Hotfix Workflow

  1. Create hotfix from main: git checkout -b hotfix/security-patch
  2. Fix critical issue: Make necessary changes
  3. Merge to main: git checkout main && git merge hotfix/security-patch
  4. Tag release: git tag -a v1.2.1 -m "Security hotfix"
  5. Push changes: git push origin main && git push origin v1.2.1
  6. Clean up: Delete hotfix branch

Release Workflow

  1. Create release branch: git checkout -b release/v2.0.0
  2. Finalize release: Version bumps, changelog, etc.
  3. Merge to main: git checkout main && git merge release/v2.0.0
  4. Tag release: git tag -a v2.0.0 -m "Release version 2.0.0"
  5. Merge back to develop: If using GitFlow
  6. Push everything: Main, develop, and tags

⭐ Best Practices

Commit Messages

Follow conventional commit format:

bash
<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

Commit Types:

  • feat: New feature
  • fix: Bug fix
  • docs: Documentation changes
  • style: Code style changes (formatting, etc.)
  • refactor: Code refactoring
  • test: Adding or updating tests
  • chore: Maintenance tasks

Examples:

bash
feat(auth): add JWT token validation

fix: resolve memory leak in image processing

docs: update API documentation for v2.0

refactor(database): optimize query performance

Branch Naming Conventions

bash
feature/feature-name
bugfix/issue-description
hotfix/critical-issue
release/version-number

đŸŽ¯ General Guidelines

  • Commit Often: Make small, focused commits
  • Write Good Messages: Clear, descriptive commit messages
  • Use Branches: Separate features, bugs, and experiments
  • Keep History Clean: Use rebase for feature branches
  • Test Before Pushing: Ensure code works before sharing
  • Pull Before Push: Always sync with remote before pushing
  • Use .gitignore: Exclude unnecessary files
  • Back Up Important Work: Push branches to remote regularly

Security Considerations

🔒 Security Best Practices

  • Never commit sensitive data (passwords, API keys, etc.)
  • Use environment variables for secrets
  • Review changes before committing
  • Use signed commits for important repositories
  • Regularly update Git and related tools
  • Be cautious with force push operations

Common .gitignore Patterns

bash
# Dependencies
node_modules/
vendor/

# Build outputs
dist/
build/
*.exe
*.dll

# IDE files
.vscode/
.idea/
*.swp

# OS files
.DS_Store
Thumbs.db

# Logs
*.log
logs/

# Environment files
.env
.env.local

# Temporary files
*.tmp
*.temp