Day 5: Advanced Git Operations

Advanced Git Operations

Git Rebase: Rewriting History

Rebasing rewrites commit history to create a linear progression. It’s powerful but requires careful handling.

Interactive Rebase:

bash

# Rebase last 3 commits interactively
git rebase -i HEAD~3

# Common rebase actions:
# pick: keep commit as-is
# reword: change commit message
# edit: pause to modify commit
# squash: combine with previous commit
# drop: remove commit entirely

Rebasing vs. Merging:

bash

# Merge approach (preserves history)
git switch main
git merge feature/new-feature

# Rebase approach (linear history)
git switch feature/new-feature
git rebase main
git switch main
git merge feature/new-feature  # Fast-forward merge

Cherry-Picking: Selective Commit Integration

bash

# Apply specific commit to current branch
git cherry-pick abc123d

# Cherry-pick range of commits
git cherry-pick abc123d..def456e

# Cherry-pick without committing (for review)
git cherry-pick --no-commit abc123d

Git Stash: Temporary Storage

bash

# Stash current changes
git stash

# Stash with descriptive message
git stash push -m "Work in progress on user dashboard"

# Stash only specific files
git stash push -m "API changes only" api/endpoints.py api/models.py

# List all stashes
git stash list

# Apply most recent stash
git stash pop

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

# Create branch from stash
git stash branch feature/dashboard-fixes stash@{1}

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

# Clear all stashes
git stash clear

Advanced Logging and Search

bash

# Search commit messages
git log --grep="bug fix"

# Search code changes
git log -S "function_name"

# Show commits by author
git log --author="John Doe"

# Show commits in date range
git log --since="2 weeks ago" --until="yesterday"

# Blame: See who changed each line
git blame filename.py

# Search within tracked files
git grep "TODO" 
git grep -n "function searchUser"  # Show line numbers
git grep -i "password"             # Case insensitive

# Find when bug was introduced with bisect
git bisect start
git bisect bad HEAD
git bisect good v1.2.0
# Git will guide you through binary search
# Test each commit Git provides
git bisect good  # or git bisect bad
# Continue until Git finds the problematic commit
git bisect reset  # Return to original state

Git Reset: Precise Undo Control

bash

# Soft reset - keep changes staged
git reset --soft HEAD~1

# Mixed reset (default) - unstage changes but keep in working directory
git reset HEAD~1
git reset --mixed HEAD~1

# Hard reset - discard all changes (DANGEROUS!)
git reset --hard HEAD~1

# Reset specific file
git reset HEAD filename.py

# Reset to specific commit
git reset --hard abc123d

Git Revert: Safe History Correction

bash

# Revert a specific commit (creates new commit)
git revert abc123d

# Revert multiple commits
git revert HEAD~3..HEAD

# Revert without committing (review first)
git revert --no-commit abc123d

# Revert a merge commit
git revert -m 1 merge-commit-hash

Git Reflog: Time Machine for Git

bash

# Show reflog for HEAD
git reflog

# Show reflog for specific branch
git reflog show feature/auth

# Reflog with timestamps
git reflog --date=iso

# Recover lost commit
git reflog
# Find the commit hash you want to recover
git reset --hard HEAD@{5}

# Create branch from reflog entry
git branch recovery-branch HEAD@{10}

Git Clean: Repository Maintenance

bash

# Show what would be removed (dry run)
git clean -n

# Remove untracked files
git clean -f

# Remove untracked files and directories
git clean -fd

# Remove ignored files too
git clean -fx

# Interactive clean
git clean -i

Git Archive: Project Snapshots

bash

# Create zip archive of current branch
git archive --format=zip --output=project-v1.0.zip HEAD

# Create tar.gz archive
git archive --format=tar.gz --output=project.tar.gz HEAD

# Archive specific directory
git archive --format=zip --output=docs.zip HEAD:docs/

# Archive with prefix
git archive --format=tar.gz --prefix=myproject/ HEAD > myproject.tar.gz

Git Tag: Version Milestones

bash

# List all tags
git tag

# Create lightweight tag
git tag v1.0.0

# Create annotated tag (recommended)
git tag -a v1.0.0 -m "Release version 1.0.0"

# Tag specific commit
git tag -a v0.9.0 abc123d -m "Beta release"

# Push tags to remote
git push origin v1.0.0
git push origin --tags  # Push all tags

# Delete tag
git tag -d v1.0.0
git push origin --delete v1.0.0  # Delete on remote

# Show tag information
git show v1.0.0

# Checkout specific tag
git checkout v1.0.0

Git Submodules: Managing Dependencies

bash

# Add submodule
git submodule add https://github.com/user/library.git lib/external

# Initialize submodules after cloning
git submodule init
git submodule update
# Or combined
git submodule update --init --recursive

# Update submodules to latest
git submodule update --remote

# Remove submodule
git submodule deinit lib/external
git rm lib/external
rm -rf .git/modules/lib/external

# Clone repository with submodules
git clone --recursive https://github.com/user/project.git

Git Hooks: Automation

bash

# Pre-commit hook example (save as .git/hooks/pre-commit)
#!/bin/bash
echo "Running pre-commit checks..."

# Run tests
python -m pytest tests/
if [ $? -ne 0 ]; then
    echo "Tests failed. Commit aborted."
    exit 1
fi

# Check code formatting
black --check .
if [ $? -ne 0 ]; then
    echo "Code formatting issues. Run 'black .' to fix."
    exit 1
fi

echo "All checks passed!"

Hands-On Exercise: Practice interactive rebase to clean up commit history, use stash for context switching, and set up a simple pre-commit hook.

You Might Also Like

🛠️ Recommended Tools for Developers & Tech Pros

Save time, boost productivity, and work smarter with these AI-powered tools I personally use and recommend:

1️⃣ CopyOwl.ai – Research & Write Smarter
Write fully referenced reports, essays, or blogs in one click.
✅ 97% satisfaction • ✅ 10+ hrs saved/week • ✅ Academic citations

2️⃣ LoopCV.pro – Build a Job-Winning Resume
Create beautiful, ATS-friendly resumes in seconds — perfect for tech roles.
✅ One-click templates • ✅ PDF/DOCX export • ✅ Interview-boosting design

3️⃣ Speechify – Listen to Any Text
Turn articles, docs, or PDFs into natural-sounding audio — even while coding.
✅ 1,000+ voices • ✅ Works on all platforms • ✅ Used by 50M+ people