@@ -22,10 +22,10 @@ func SyncCmd(cfg *config.Config) *cobra.Command {
2222
2323This command performs a safe, non-interactive synchronization:
2424
25- 1. Fetches the latest changes from origin
25+ 1. Fetches the latest changes from the remote
2626 2. Fast-forwards the trunk branch to match the remote
2727 3. Cascade-rebases stack branches onto their updated parents
28- 4. Pushes all branches (using --force-with-lease)
28+ 4. Pushes all branches atomically (using --force-with-lease --atomic )
2929 5. Syncs PR state from GitHub
3030
3131If a rebase conflict is detected, all branches are restored to their
@@ -75,22 +75,29 @@ func runSync(cfg *config.Config, _ *syncOptions) error {
7575 return nil
7676 }
7777
78+ // Resolve remote once for fetch and push
79+ remote , err := pickRemote (cfg , currentBranch )
80+ if err != nil {
81+ cfg .Errorf ("%s" , err )
82+ return nil
83+ }
84+
7885 // --- Step 1: Fetch ---
7986 // Enable git rerere so conflict resolutions are remembered.
8087 _ = git .EnableRerere ()
8188
82- if err := git .Fetch ("origin" ); err != nil {
83- cfg .Warningf ("Failed to fetch origin : %v" , err )
89+ if err := git .Fetch (remote ); err != nil {
90+ cfg .Warningf ("Failed to fetch %s : %v" , remote , err )
8491 } else {
85- cfg .Successf ("Fetched latest changes" )
92+ cfg .Successf ("Fetched latest changes from %s" , remote )
8693 }
8794
8895 // --- Step 2: Fast-forward trunk ---
8996 trunk := s .Trunk .Branch
9097 trunkUpdated := false
9198
9299 localSHA , localErr := git .HeadSHA (trunk )
93- remoteSHA , remoteErr := git .HeadSHA ("origin /" + trunk )
100+ remoteSHA , remoteErr := git .HeadSHA (remote + " /" + trunk )
94101
95102 if localErr != nil || remoteErr != nil {
96103 cfg .Warningf ("Could not compare trunk %s with remote — skipping trunk update" , trunk )
@@ -101,13 +108,12 @@ func runSync(cfg *config.Config, _ *syncOptions) error {
101108 if err != nil {
102109 cfg .Warningf ("Could not determine fast-forward status for %s: %v" , trunk , err )
103110 } else if ! isAncestor {
104- cfg .Warningf ("Trunk %s has diverged from origin — skipping trunk update" , trunk )
111+ cfg .Warningf ("Trunk %s has diverged from %s — skipping trunk update" , trunk , remote )
105112 cfg .Printf (" Local and remote %s have diverged. Resolve manually." , trunk )
106113 } else {
107114 // Fast-forward the trunk branch
108115 if currentBranch == trunk {
109- // Can't update ref of checked-out branch; merge instead
110- if err := ffMerge (trunk ); err != nil {
116+ if err := git .MergeFF (remote + "/" + trunk ); err != nil {
111117 cfg .Warningf ("Failed to fast-forward %s: %v" , trunk , err )
112118 } else {
113119 cfg .Successf ("Trunk %s fast-forwarded to %s" , trunk , short (remoteSHA ))
@@ -231,12 +237,7 @@ func runSync(cfg *config.Config, _ *syncOptions) error {
231237
232238 // --- Step 4: Push ---
233239 cfg .Printf ("" )
234- var branches []string
235- for _ , b := range s .Branches {
236- if ! b .IsMerged () {
237- branches = append (branches , b .Branch )
238- }
239- }
240+ branches := activeBranchNames (s )
240241
241242 if mergedCount := len (s .MergedBranches ()); mergedCount > 0 {
242243 cfg .Printf ("Skipping %d merged %s" , mergedCount , plural (mergedCount , "branch" , "branches" ))
@@ -248,8 +249,8 @@ func runSync(cfg *config.Config, _ *syncOptions) error {
248249 // After rebase, force-with-lease is required (history rewritten).
249250 // Without rebase, try a normal push first.
250251 force := rebased
251- cfg .Printf ("Pushing branches ..." )
252- if err := git .Push ("origin" , branches , force , false ); err != nil {
252+ cfg .Printf ("Pushing %d %s to %s ..." , len ( branches ), plural ( len ( branches ), "branch" , "branches" ), remote )
253+ if err := git .Push (remote , branches , force , true ); err != nil {
253254 if ! force {
254255 cfg .Warningf ("Push failed — branches may need force push after rebase" )
255256 cfg .Printf (" Run %s to push with --force-with-lease." ,
@@ -293,19 +294,7 @@ func runSync(cfg *config.Config, _ *syncOptions) error {
293294 }
294295
295296 // --- Step 6: Update base SHAs and save ---
296- for i := range s .Branches {
297- // Skip merged branches when updating base SHAs.
298- if s .Branches [i ].IsMerged () {
299- continue
300- }
301- parent := s .ActiveBaseBranch (s .Branches [i ].Branch )
302- if base , err := git .HeadSHA (parent ); err == nil {
303- s .Branches [i ].Base = base
304- }
305- if head , err := git .HeadSHA (s .Branches [i ].Branch ); err == nil {
306- s .Branches [i ].Head = head
307- }
308- }
297+ updateBaseSHAs (s )
309298
310299 if err := stack .Save (gitDir , sf ); err != nil {
311300 cfg .Errorf ("failed to save stack state: %s" , err )
0 commit comments