# Git: branches, rebase and merge conflicts

This guide builds on *Working with Git*. You already know how `commit`, `push` and `pull` work. Now you’ll learn how to work safely with multiple changes at the same time.

## Working with a branch

A **branch** is a separate branch where you can try things out without affecting the main version (`main`).

```bash
git switch main
git pull                      # eerst de nieuwste versie ophalen
git switch -c feature/factuur-pdf
# ... werken en committen ...
git push -u origin feature/factuur-pdf
```

Next, open a **pull request** on GitHub. This allows someone (or a reviewer) to review your changes before they are merged into `main`.

Use clear names: `feature/...` for new features, `fix/...` for fixes.

## Merge or rebase?

Whilst you’re working, `main` also changes. There are two ways to incorporate those changes into your branch:

| | `git merge main` | `git rebase main` |
|---|---|---|
| What happens | An extra "merge commit" is added | Your commits are merged back into `main` |
| History | Shows exactly what happened | Clear and well-organised |
| Risk | Low | Rewrites history |

**Rule of thumb:** only rebase a branch that you are the only one working on. Never rebase `main` or a branch that others are using.

```bash
git switch feature/factuur-pdf
git fetch origin
git rebase origin/main
git push --force-with-lease
```

After a rebase, you must push using `--force-with-lease`. This is the safe version of `--force`: it will refuse if someone else has pushed something in the meantime.

If something goes wrong during a rebase, `git rebase --abort` will restore everything to how it was.

## Resolving a merge conflict

A conflict arises when you and someone else have both changed the same rules. Git then cannot determine which version is correct. In the file, you will see:

```
<<<<<<< HEAD
$prijs = $bedrag * 1.21;
=======
$prijs = $bedrag * $btw;
>>>>>>> feature/btw-instelling
```

In **VS Code**, buttons appear above the conflict: *Accept Current*, *Accept Incoming*, *Accept Both*. Choose the correct version, or edit the code manually to ensure it is correct. Then:

```bash
git add bestand.php
git rebase --continue    # of: git commit (bij een merge)
```

Always test your app after a conflict. Feel free to ask your AI assistant to explain the conflict, but check what it chooses.

## Good commit messages

Write, in the imperative, what the commit does:

```
feat(facturen): voeg PDF-download toe
fix(checkout): voorkom dubbele betaling bij herhaalde webhook
```

Many teams add an emoji at the start, such as ✨ for ‘new’ or 🐛 for a ‘fix’. The **conventional-commit** and **gitmoji-status-commits** skills allow your assistant to do this consistently. See also *Commit messages and status emojis*.

## Worktrees: multiple sectors at the same time

With a **work tree**, you can have two branches open at the same time in separate folders. This is handy if your AI assistant is working on a feature whilst you’re fixing a bug:

```bash
git worktree add ../mijnapp-fix fix/login-bug
# klaar:
git worktree remove ../mijnapp-fix
```

## Useful commands

| Command | What it does |
|---|---|
| `git log --oneline --graph` | History as a tree |
| `git stash` / `git stash pop` | Temporarily stash / restore changes |
| `git cherry-pick <hash>` | Pick a single commit from another branch |
| `git revert <hash>` | Undo a commit with a new commit (safe) |
| `git bisect` | Find the commit that caused a bug |

Be careful with `git reset --hard`: it will discard your uncommitted work.

## Need help?

Would you like every pull request from you or your agent to be reviewed before it goes live? Take a look at **AI code review**.

## Skills

- [Git Advanced Workflows] (/nl/skills/git-advanced-workflows)
- [Using Git Worktrees] (/nl/skills/using-git-worktrees)
- [Finishing a Development Branch] (/nl/skills/finishing-a-development-branch)
- [Conventional Commit] (/nl/skills/conventional-commit)
- [Emoji status in commits, PRs and updates] (/nl/skills/gitmoji-status-commits)

## Related services

- [Beoordeling van AI-code] (/nl/services#service-05)
- [Je eigen programmeeragent] (/nl/services#service-06)

