Git Cheat Sheet Series (4/5): Conflicts + Multi-Branch Reality

Git Cheat Sheet Series (4/5): Conflicts + Multi-Branch Reality
Flat illustration of resolving rebase conflicts, squashing and splitting commits, and handling a hotfix from main back into dev.

Short scenarios. Copy/paste commands. Minimal notes.

Rebase your feature on dev and resolve conflicts

Goal: Update your branch with latest dev while keeping history linear.

git fetch origin
git checkout feat/<feature-name>
git rebase origin/dev

# if conflicts:
# edit conflicted files
git add -A
git rebase --continue

Notes

  • Abort the whole rebase: git rebase --abort
  • Skip a problematic commit: git rebase --skip (rare, be sure)

Squash commits before merging (clean PR)

Goal: Turn many “WIP” commits into one (or a few) clean commits.

git fetch origin
git checkout feature/<short-name>
git rebase -i origin/dev
# mark extra commits as "squash" or "fixup"

Notes

  • After rewriting history you usually need:
    • git push --force-with-lease

Split one commit into two

Goal: One commit contains unrelated changes; separate them for review.

git checkout feat/<feature-name>
git rebase -i HEAD~<N>
# mark the target commit as "edit"

git reset HEAD^
# stage part 1
git add -p
git commit -m "Part 1: <message>"

# stage part 2
git add -A
git commit -m "Part 2: <message>"

git rebase --continue

Notes

  • git add -p lets you stage hunks interactively.

Hotfix workflow: patch main, then bring fix into dev

Goal: A production fix must go to main first, then back to dev.

# hotfix branch from main
git checkout main
git pull --ff-only
git checkout -b hotfix/<short-name>

# commit the fix
git add -A
git commit -m "Hotfix: <message>"
git push -u origin hotfix/<short-name>
# open PR -> main

# after merge to main, sync dev and bring fix in
git checkout dev
git pull --ff-only

# option A: merge main into dev (common)
git merge origin/main
git push

# option B: cherry-pick the fix commit into dev (targeted)
# git cherry-pick <fix-sha>
# git push

Notes

  • Choose A (merge) when you want dev to fully include main changes.
  • Choose B (cherry-pick) for a minimal, targeted backport.

Make a clean PR when your branch got messy

Goal: Start fresh from dev and move only the good commits.

git fetch origin
git checkout -b feature/<clean-name> origin/dev

# bring over only the commits you want
git cherry-pick <sha1> <sha2> <sha3>

git push -u origin feature/<clean-name>
# open PR from feature/<clean-name> -> dev

Notes

  • This avoids dragging accidental merges/rebases into your PR history.

Read more