> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cubic.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Keep your stack updated

> Sync with trunk, restack branches, and handle conflicts.

When your trunk (`main`/`master`) changes—or you need to revise something mid-stack—**mg** streamlines the process with commands like `mg sync`, `mg modify`, and more.

<Note>
  **Prerequisites** - [Create a stack](/how-to-guides/create-a-stack) - Familiarity with `mg push`
  and basic Git conflict resolution
</Note>

***

## 1. Sync with remote changes

```bash theme={null}
mg sync
```

<Steps>
  <Step title="Fetch new commits">mg fetches new commits from `main`.</Step>
  <Step title="Clean up merged branches">mg prompts to delete merged branches.</Step>
  <Step title="Restack branches">mg restacks any child branches on top of the updated trunk.</Step>
</Steps>

<Warning>
  If conflicts appear, follow the on-screen instructions to resolve them and run `mg continue`.
</Warning>

***

## 2. Update mid-stack branches

<Steps>
  <Step title="Switch to the branch">`bash mg checkout feature_2 `</Step>

  <Step title="Make changes and commit">
    `bash mg add . mg commit -m "Address feedback" # or mg modify to amend `
  </Step>

  <Step title="Handle restacking">
    mg automatically re-stacks child branches on top. If you see conflicts, fix them, `mg add .`,
    then `mg continue`.
  </Step>
</Steps>

***

## 3. Push again

After local changes:

```bash theme={null}
mg push
```

Or push everything at once:

```bash theme={null}
mg push --all
```

Your PRs update on cubic.dev / GitHub accordingly.

***

## 4. Orphan / adopt branches

<Steps>
  <Step title="Orphan a branch">
    Remove a branch from the stack but keep it in Git:

    ```bash theme={null}
    mg orphan <branch>
    ```
  </Step>

  <Step title="Adopt a branch">
    Bring an existing Git branch into mg's stack:

    ```bash theme={null}
    mg adopt <branch>
    ```
  </Step>
</Steps>

***

## 5. Splitting or folding commits

<Steps>
  <Step title="Split commits">Breaks large commits into smaller ones: `bash mg split `</Step>

  <Step title="Fold/squash commits">
    If you want to combine commits: `bash mg split --fold `
  </Step>
</Steps>

***

## Done!

Your stack is in sync with trunk. Each branch can be pushed or merged independently.

<Tip>
  * **Ready to merge?** See [Merge your stack](/how-to-guides/merge-your-stack). -
    **Troubleshooting?** Check our [FAQ](/faq-and-troubleshooting).
</Tip>

**Happy syncing!**
