@@ -26,16 +26,27 @@ func SyncCmd(cfg *config.Config) *cobra.Command {
2626 Short : "Sync the current stack with the remote" ,
2727 Long : `Fetch, rebase, push, and sync PR state for the current stack.
2828
29- This command performs a safe, non-interactive synchronization:
29+ This command performs a safe synchronization:
3030
3131 1. Fetches the latest changes from the remote
32- 2. Fast-forwards the trunk branch to match the remote
33- 3. Cascade-rebases stack branches onto their updated parents
34- 4. Pushes all branches atomically (using --force-with-lease --atomic)
35- 5. Syncs PR state from GitHub
36- 6. Links the stack's open PRs into a stack on GitHub (creating or updating
32+ 2. Reconciles the stack on GitHub with your local stack: pulls down
33+ branches for any PRs added to the stack on GitHub, or prompts you to
34+ resolve a divergence in an interactive terminal
35+ 3. Fast-forwards the trunk branch to match the remote
36+ 4. Cascade-rebases stack branches onto their updated parents
37+ 5. Pushes all branches atomically (using --force-with-lease --atomic)
38+ 6. Syncs PR state from GitHub
39+ 7. Links the stack's open PRs into a stack on GitHub (creating or updating
3740 the remote stack object) when two or more PRs exist
3841
42+ If PRs have been added to the stack on GitHub, their branches are pulled
43+ down and appended to your local stack so it mirrors the remote. A clean
44+ "remote is ahead" update happens automatically without prompting. If the
45+ local and remote stacks have diverged, sync prompts (in an interactive
46+ terminal) to use the remote as the source of truth, use your local stack
47+ as the source of truth, disassociate them, or cancel. In a non-interactive
48+ terminal a divergence is reported and left untouched.
49+
3950If a rebase conflict is detected, all branches are restored to their
4051original state and you are advised to run "gh stack rebase" to resolve
4152conflicts interactively.
@@ -45,7 +56,7 @@ links PRs that already exist. The final message reflects what happened:
4556"Stack synced" means the stack object on GitHub now matches your local
4657stack, while "Branches synced" means the branches were rebased and pushed
4758but no remote stack object was created or updated (for example, when fewer
48- than two PRs exist yet).
59+ than two PRs exist yet, or a divergence was left unresolved ).
4960
5061Use --prune to delete local branches for merged PRs. Stack metadata is
5162preserved so that rebase and display logic continue to work correctly.
@@ -99,6 +110,22 @@ func runSync(cfg *config.Config, opts *syncOptions) error {
99110 _ = git .FetchBranches (remote , fetchTargets )
100111 cfg .Successf ("Fetched latest changes from %s" , remote )
101112
113+ // --- Step 1b: Reconcile remote-ahead stack changes ---
114+ // Pull in branches for PRs that were added to the stack on GitHub, or
115+ // resolve a divergence, before rebasing and pushing so pulled branches
116+ // participate in the normal flow. Best-effort for stacks tracked on the
117+ // remote; a no-op otherwise.
118+ reconcileRes , err := reconcileRemoteStack (cfg , sf , s , gitDir , remote )
119+ if err != nil {
120+ if errors .Is (err , errInterrupt ) {
121+ return ErrSilent
122+ }
123+ return err
124+ }
125+ if reconcileRes .stack != nil {
126+ s = reconcileRes .stack
127+ }
128+
102129 // --- Step 2: Fast-forward trunk ---
103130 trunk := s .Trunk .Branch
104131 trunkUpdated := fastForwardTrunk (cfg , trunk , remote , currentBranch )
@@ -237,8 +264,10 @@ func runSync(cfg *config.Config, opts *syncOptions) error {
237264 // stack object actually reflects the local stack, which determines the final
238265 // summary message below.
239266 stackSynced := false
240- if client , err := cfg .GitHubClient (); err == nil {
241- stackSynced = syncStack (cfg , client , s )
267+ if ! reconcileRes .skipStackObjectSync {
268+ if client , err := cfg .GitHubClient (); err == nil {
269+ stackSynced = syncStack (cfg , client , s )
270+ }
242271 }
243272
244273 // --- Step 6: Prune merged branches (optional) ---
0 commit comments