learning

Phase 16: Git

1067 words6 min read
Phase 16: Git
Authors

Welcome to the comprehensive guide on Advanced Git.

When you first learn Git, you learn the holy trinity: git add ., git commit -m "update", and git push.

The Analogy: Using basic Git is like using a video game save slot. Before you fight a boss, you save the game. If you die, you reload the save.

However, in a professional engineering environment, you aren't playing a single-player game. You are playing a massive multiplayer game where 50 developers are trying to save their progress into the same save file simultaneously. If you only know the basics, your project history will turn into a tangled, incomprehensible spaghetti of messy merges.

In this textbook-level tutorial, we will elevate you from a Git beginner to a version control wizard capable of rewriting history and rescuing lost code.


Part 1: Advanced Collaboration and Rewriting History

When working on a feature branch (feature/login), the main branch continues to move forward as other developers merge their code. Eventually, your branch falls behind. How do you integrate those new changes into your branch?

1. Merge vs. Rebase

The traditional git merge main: Merging takes the new commits from main and creates a special "Merge Commit" in your branch to tie the two histories together.

  • Pros: Safe, non-destructive.
  • Cons: If you merge frequently, your Git history graph starts to look like a chaotic spiderweb of crisscrossing lines.

The professional git rebase main: Rebasing is a magical operation. It literally picks up your feature branch, temporarily sets your new commits aside, updates your branch to exactly match main, and then re-applies your new commits on top of it.

  • Pros: Creates a perfectly linear, clean, easily readable history.
  • Cons: It rewrites history. If you rebase a branch that other developers are currently working on, you will cause chaos. Golden Rule: Only rebase your own local branches.
# How to rebase your feature branch against the latest main
git checkout feature/login

# Fetch latest changes from the remote server
git fetch origin

# Rebase your local branch on top of origin/main
git rebase origin/main

# Because you rewrote history, you must force push to the remote
git push -f origin feature/login

2. Cherry-Picking

The Analogy: Picking the perfect strawberry out of a giant basket.

The Concept: Sometimes you don't want to merge an entire branch; you just want one specific commit. For example, your coworker fixed a critical bug on their branch, and you need that exact fix in your branch right now.

# Find the commit hash of the bug fix (e.g., a1b2c3d)
git log

# Make sure you are on your branch
git checkout feature/my-work

# Copy that specific commit over to your branch!
git cherry-pick a1b2c3d

3. The Git Stash

The Concept: You are halfway through writing a complex function. Suddenly, your manager tells you to drop everything and fix a typo on the main branch immediately. You aren't ready to commit your half-broken code, but you can't switch branches with uncommitted changes.

Enter the stash.

# Shove all uncommitted work into a temporary hidden drawer
git stash

# Now your working directory is clean! Switch to main and fix the typo.
git checkout main
# ... fix and push ...

# Come back to your branch
git checkout feature/login

# Open the drawer and put your messy code back exactly where you left it!
git stash pop

Part 2: Rescuing Yourself from Disaster

Everyone messes up in Git. You will accidentally delete a branch. You will botch a rebase. When this happens, do not panic and do not delete the entire folder to clone it again. Use the safety nets.

1. Undo with git reset

If you made a commit, but realize you forgot to add a file or made a typo in the commit message.

  • git reset --soft HEAD~1: Un-commits your last commit, but keeps all your files completely intact and staged. It just steps back in time one notch.
  • git reset --hard HEAD~1: DANGER. Completely wipes out the last commit and permanently deletes the file changes you made. Use with extreme caution.

2. The Ultimate Safety Net: git reflog

The Concept: git reflog (Reference Log) is Git's secret black box flight recorder. Every time the tip of your branch changes (a commit, a rebase, a reset, a merge), Git secretly records it in the reflog.

If you accidentally run git reset --hard and delete a week of work, git log won't show the commits anymore. But git reflog remembers.

# View the secret history
git reflog
# Output looks like:
# 3f4a1c HEAD@{0}: reset: moving to HEAD~1 (The mistake!)
# 9b2e8d HEAD@{1}: commit: finished the giant feature (The code you want back!)

# Time-travel back to the moment before the mistake
git reset --hard HEAD@{1}

Your code is magically restored.

3. Finding Bugs with git bisect

The Concept: A bug was introduced sometime in the last 100 commits, but you don't know which one. git bisect uses a binary search algorithm to find the exact commit that broke the code in minutes.

git bisect start
git bisect bad # Tell Git the current commit has the bug
git bisect good 9b2e8d # Tell Git an older commit hash that you know worked fine

# Git will now checkout a commit exactly halfway between the good and bad.
# You test the app.
# If it's broken, type: git bisect bad
# If it works, type: git bisect good
# Git will keep cutting the commits in half until it pinpoints the exact line of code that caused the bug!

Part 3: Git Hooks

The Concept: A Git Hook is a script that Git executes automatically before or after a specific event (like committing or pushing).

In professional teams, developers are forgetful. They forget to run code formatters (Prettier) or Linters (ESLint) before committing.

Instead of yelling at developers, we use a Pre-commit Hook (usually managed via a Node.js tool called Husky).

When a developer types git commit -m "msg", the Pre-commit Hook automatically intercepts the command, runs the linter, formats the code, and runs the test suite. If the tests fail, the hook blocks the commit and says "Fix your tests first!" This ensures bad code never even enters the version control system.

Tags

#git#vcs