← All writing

Production Git Mastery: Interactive Rebase, Merge vs. PR/MR Workflows, Cherry-Pick & Reflog

· Sourabh G Kulkarni

Master production Git operations: interactive rebase, merge vs PR/MR workflows, git cherry-pick, detached HEAD recovery, and git reflog safety nets.

Most developers learn just enough Git to survive: git add ., git commit -m "fix", git pull, and git push.

And for basic personal projects, that is enough. But the moment you join an engineering team with fifty engineers deploying to production multiple times a day, basic Git habits break down. Commit histories turn into tangled webs of recursive merge commits, release branches diverge, accidental commits end up in main, and panic sets in when a bad command seemingly deletes three days of unpushed work.

Git is not a magical versioning tool; it is a content-addressable Directed Acyclic Graph (DAG) of cryptographic SHA-1/SHA-256 snapshots. Once you understand the graph mechanics, operations like rebasing, cherry-picking, and disaster recovery via reflog become completely predictable.

Here is a practical guide to mastering the Git commands you actually need in production.


1. Merge vs. Rebase: What Actually Happens to the Graph?

The debate between git merge and git rebase is often argued on aesthetics: "Rebase keeps the history clean; merge preserves the true timeline."

To understand the difference, look at what happens to the underlying graph pointers.

Imagine you branched feature off main at commit B. While you worked on commits C and D, your teammate pushed commits E and F to main:

      C ─── D (feature)
     /
A ── B ── E ── F (main)

Option 1: git merge main

A merge creates a brand new Merge Commit (M) with two parent pointers:

      C ─── D ───────┐
     /                ▼
A ── B ── E ── F ─── M (feature)
  • Pros: Preserves the exact chronological history of when branches diverged.
  • Cons: In high-traffic repos, the commit history becomes cluttered with dozens of "Merge branch 'main' into feature" commits that obscure meaningful changes.

Option 2: git rebase main

A rebase rewinds your branch, moves the base pointer to the tip of main (F), and replays your commits (C', D') on top as brand-new commits with new SHA hashes:

A ── B ── E ── F (main) ─── C' ─── D' (feature)
  • Pros: A clean, linear commit history where every change can be cleanly inspected, bisected (git bisect), and reverted with zero clutter.
  • Cons: Modifies commit history (rewriting SHAs). The Golden Rule: Never rebase a public branch that other engineers are actively pulling from.

2. Interactive Rebase (git rebase -i): Cleaning Up Before Review

Before you open a Pull Request (GitHub) or Merge Request (GitLab), your local commit history might look like this:

commit 4a91b2c - WIP
commit 8d2e1f4 - typo fix
commit 9b3a4c1 - add stripe integration
commit 7e1d5a2 - fix tests again
commit 1f4a9b2 - debug log

Submitting five messy, uninformative commits to a code reviewer wastes team time. With Interactive Rebase, you can squash, edit, reorder, or drop commits before opening the PR:

git rebase -i HEAD~5

Git opens an editor showing your commit list:

pick 9b3a4c1 add stripe integration
squash 4a91b2c WIP
squash 8d2e1f4 typo fix
squash 7e1d5a2 fix tests again
drop 1f4a9b2 debug log
  • pick: Keep this commit.
  • squash (or s): Meld this commit into the previous commit and combine their messages.
  • drop (or d): Permanently delete this commit from the branch.

Save and close the file. Git folds your five messy commits into a single, polished commit: "Add Stripe billing integration with webhook verification".


3. Pull Request (PR) vs. Merge Request (MR): Is There a Difference?

Engineers often ask: "What is the architectural difference between a GitHub Pull Request and a GitLab Merge Request?"

Functionally, they solve the exact same problem: facilitating peer code review before integrating code into a target branch.

  • The GitHub Terminology (Pull Request): Originating from open-source Git workflows, you are asking the maintainer: "Please pull my branch into your repository."
  • The GitLab Terminology (Merge Request): GitLab emphasizes the final action: "I am requesting that my completed changes be merged into the target branch."

Modern Merge Strategies in PRs/MRs

When merging a PR/MR via the UI, you typically choose among three strategies:

  1. Create a Merge Commit (--no-ff): Keeps all commits from the branch and adds a merge commit.
  2. Squash and Merge: Compresses all branch commits into a single commit on main. (Best for keeping main history clean and atomic).
  3. Rebase and Merge: Fast-forwards commits linearly onto main without creating a merge commit.

4. git cherry-pick: Surgical Extraction

Imagine a critical security vulnerability or zero-day bug is discovered in production. A junior developer fixed it inside an in-progress feature branch containing twenty other unfinished, untested changes.

You cannot merge the entire feature branch into production. You only want that one specific bugfix commit (a1b2c3d).

This is where git cherry-pick shines:

# 1. Switch to your production release branch
git checkout release/v2.4

# 2. Pull the latest commits
git pull origin release/v2.4

# 3. Surgically copy and apply that single commit onto your current branch
git cherry-pick a1b2c3d

# 4. Push directly to production hotfix
git push origin release/v2.4

Git examines the diff introduced by commit a1b2c3d and applies it as a brand-new commit on top of release/v2.4, leaving the rest of the unready feature branch behind.


5. git reflog: The Time Machine Safety Net

The biggest fear with advanced Git commands (like rebase, reset --hard, or deleting branches) is losing code forever.

Here is the secret: Git almost never deletes anything immediately.

When you delete a branch or perform a hard reset, Git simply updates a branch pointer. The underlying commit objects still exist in .git/objects as "dangling commits" for at least 30 days before garbage collection (git gc) purges them.

git reflog records every single movement of your HEAD pointer, even when commits are not part of any active branch:

git reflog

Output:

3a1f9e2 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~2
8b2d4c1 HEAD@{1}: commit: add payment gateway logic
9f1e2a4 HEAD@{2}: commit: fix auth middleware

If you accidentally ran git reset --hard HEAD~2 and lost your payment gateway code:

  1. Locate the commit hash before the mistake (8b2d4c1 at HEAD@{1}).
  2. Restore it immediately:
    git reset --hard 8b2d4c1
    

Your seemingly deleted work is back in zero seconds.


Summary Cheat Sheet

CommandWhen to Use It
git rebase mainKeep feature branch updated with clean, linear history
git rebase -i HEAD~NSquash messy WIP commits before opening a PR
git cherry-pick <SHA>Surgically pull a single commit from one branch to another
git reflogView every HEAD movement to recover "deleted" commits or branches
git commit --amendFix the commit message or add forgotten files to the latest commit