Menu

The Ultimate Git Command Guide:
From Merge vs Rebase to Conflict Resolution 🍎

Minimalist developer setup showing a MacBook screen with a Git terminal and clean code editor UI elements

Are you comfortable with basic commands like git add, commit, and push? If so, it's time to remove your "newbie tag". In real-world collaboration, branches get tangled, dreadful conflicts occur, and sometimes you face crises where you need to roll back entire chunks of code. Today, we'll dive deep into advanced Git techniques used by professionals, delivered through an intuitive and elegant Apple-style UI. 🚀

1. Workflows: Git Flow vs GitHub Flow

How should you merge and deploy code? Your branching strategy depends on your team's size and product characteristics.

🏢 Git Flow (Traditional & Complex)

Ideal for large enterprise environments or projects with strict release cycles.

  • main: Production-ready state
  • develop: Latest delivered development changes
  • feature: Developing new features (branch off develop)
  • release: Pre-production QA (bug fixes)
  • hotfix: Emergency bug fixes on production

🚀 GitHub Flow (Modern & Agile)

Perfect for startups with strong CI/CD or web services that deploy multiple times a day.

  • main: Always deployable!
  • feature: Branch off main for new features. Open a PR, merge to main, and deploy immediately.
  • No complex develop or release branches.
"Rebase is powerful magic for rewriting history. But you should never cast this spell on a branch shared with others." – Senior DevOps Engineer

2. The Eternal Debate: Merge vs Rebase

There are two primary ways to integrate changes from one branch into another. The resulting history is entirely different.

🔀 git merge : "Preserve History As Is"

Combines the histories of two branches, creating a new 'Merge Commit'. Every branching point is preserved like a family tree.

# Switch to main branch
git switch main

# Merge feature into main (creates a merge commit)
git merge feature-branch

✂️ git rebase : "A Clean, Linear History"

Moves the starting point (base) of your branch to the latest commit of the target branch. No merge commit is left, resulting in a clean, straight-line history.

# While on your feature branch
git switch feature-branch

# Re-apply your commits on top of the latest main
git rebase main

# WARNING: Never rebase a branch you've already pushed!

3. Don't Panic! Resolving Merge Conflicts

A Conflict occurs when two developers modify the exact same line in the same file. Don't worry; there is a formula to solve this.

🚨 The 4-Step Conflict Resolution Process

# 1. Check status: Look for red files marked as 'both modified'.
git status

# 2. Open your code editor (e.g., VS Code) and fix the conflict.
# Choose between <<<<<<< HEAD (your code) and >>>>>>> (incoming code).
# Keep what you need and delete the conflict markers (<<<, ===, >>>).

# 3. Stage the resolved file.
git add resolved_file.js

# 4. Continue the merge or rebase process.
git commit -m "fix: resolve merge conflict" # If Merging
git rebase --continue # If Rebasing

# 🔥 When it's too tangled and you want to give up (abort merge)
git merge --abort

4. Advanced Lifesaver Commands

These are advanced skills for when you've blown up your code or just want to "steal" a specific commit from someone else's branch.

  • 🍒 Cherry-pick: Grab one specific commit from another branch and apply it to yours.
    git cherry-pick <commit-hash>
  • 📦 Stash: Temporarily shelve your uncommitted work. Perfect for when you urgently need to switch branches.
    git stash (Save) ➔ git stash pop (Retrieve)
  • ✏️ Amend: Modify the very last commit if you made a typo in the message or forgot to add a file.
    git commit --amend
  • 🚑 Reflog: The Ultimate Time Machine. A log of every action you've taken in Git. You can even recover commits you deleted with a hard reset.
    git reflog (Find hash) ➔ git reset --hard <hash>

Conclusion: Quick Summary Table

Situation / Goal Command Notes
Merge & preserve history git merge <branch> Creates a Merge Commit
Clean, linear merge git rebase <branch> Never use on public branches
Cancel a conflicting merge git merge --abort Restores pre-merge state
Temporarily save work git stash Hides unstaged changes
View all action history git reflog The last hope for recovery
Share:
Home Search Share Link