# Branching & Rebase Cheat Sheet

From Modern Git Academy — https://moderngitacademy.com/

---

## The mental model

A branch is **a movable pointer to a commit**. Nothing more. Creating one is instant and
costs nothing, because it writes 41 bytes to a file.

`HEAD` is a pointer to the branch you are on.

## Merge vs rebase

|  | Merge | Rebase |
| --- | --- | --- |
| History | Preserved exactly | Rewritten |
| Commit SHAs | Unchanged | **All new** |
| Result shape | Branching graph | Linear |
| Safe on shared branches | Yes | **No** |
| Conflict resolution | Once | Potentially once per commit |

### The rule

> **Rebase before you share. Merge after.**

Rebasing commits that other people already have rewrites history they hold. Their next pull
produces a mess.

## Rebase workflow

```
git switch feature
git fetch origin
git rebase origin/main
# resolve conflicts, then:
git add <resolved-files>
git rebase --continue
git push --force-with-lease
```

⚠️ `--force-with-lease`, never `--force`. It refuses if the remote has commits you have not
seen — which is exactly the case where `--force` destroys someone's work.

## Interactive rebase

```
git rebase -i main
```

| Verb | Effect |
| --- | --- |
| `pick` | Keep the commit as is |
| `reword` | Keep changes, edit the message |
| `squash` | Merge into previous, combine messages |
| `fixup` | Merge into previous, discard this message |
| `edit` | Stop so you can amend |
| `drop` | Remove the commit |

Lines are applied **top to bottom** — oldest first. This is the opposite order to `git log`.

## Escape hatches

```
git rebase --abort       # abandon, restore original branch
git merge --abort        # abandon an unresolved merge
git reflog               # find where you were before any of it
```

## Conflict resolution

```
git status                    # which files conflict
git diff                      # see the conflict markers
# edit files, remove <<<<<<< ======= >>>>>>> markers
git add <file>
git rebase --continue         # or: git merge --continue
```

⚠️ Stage your resolution before `--continue`. With nothing staged and no changes, Git treats
it as an empty commit.

## Branching strategies

| Strategy | Fits |
| --- | --- |
| **Trunk-based** | Continuous delivery, strong test coverage, small changes |
| **GitHub Flow** | Web services deploying frequently from `main` |
| **Git Flow** | Versioned software with maintained release lines |

Most teams choosing Git Flow in 2026 would be better served by trunk-based development.
The release branches solve a problem that continuous deployment removes.

---

Full lessons: https://moderngitacademy.com/workflows/branching/
