Skip to content

Commit 2ca3c5e

Browse files
committed
fix(git): fix branch switching and upstream tracking on new branches
- Push new branches with upstream tracking after creation to avoid detached state - Fetch specific branch refs before checkout to ensure up-to-date remote info - Add checks to avoid reset errors when remote branch is missing or inaccessible - Improve branch reset logic to handle missing remote refs gracefully and continue local checkout
1 parent 1ad26e0 commit 2ca3c5e

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

ui/gitRoutes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,15 @@ export function setupGitRoutes(app, isAuthenticated, dependencies) {
533533
// Create new branch from current branch
534534
await git.checkoutLocalBranch(branch);
535535

536+
// Push the new branch with upstream tracking
537+
try {
538+
await git.push(['-u', 'origin', branch]);
539+
console.log(`Successfully pushed new branch '${branch}' with upstream tracking`);
540+
} catch (pushError) {
541+
console.log(`Warning: Could not push new branch '${branch}' to remote:`, pushError.message);
542+
// Continue anyway - the branch was created locally
543+
}
544+
536545
return res.json({
537546
success: true,
538547
currentBranch: branch,

ui/repositoryManager.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ export async function ensureRepositoryCheckout(options) {
116116
console.log('Fetched latest updates from remote');
117117

118118
if (branch) {
119+
// Ensure specific branch reference is up-to-date
120+
try {
121+
await git.fetch(['origin', branch]);
122+
console.log(`Refreshed remote reference for branch: ${branch}`);
123+
} catch (fetchError) {
124+
console.log(`Note: Could not fetch specific branch '${branch}':`, fetchError.message);
125+
// Continue anyway - the branch might not exist on remote yet
126+
}
127+
119128
// Handle branch-specific operations
120129
const branches = await git.branch();
121130
const localBranchExists = branches.all.includes(branch);
@@ -126,8 +135,21 @@ export async function ensureRepositoryCheckout(options) {
126135
await git.checkout(branch);
127136
console.log(`Switched to branch: ${branch}`);
128137
if (forceReset) {
129-
await git.reset(['--hard', `origin/${branch}`]);
130-
console.log(`Reset to origin/${branch}`);
138+
// Verify remote reference exists before attempting reset
139+
try {
140+
const remoteRefs = await git.raw(['ls-remote', '--heads', 'origin', branch]);
141+
if (!remoteRefs.trim()) {
142+
console.log(`Warning: Remote branch 'origin/${branch}' not found, skipping reset`);
143+
} else {
144+
// Ensure we have the latest remote reference locally
145+
await git.fetch(['origin', `${branch}:refs/remotes/origin/${branch}`]);
146+
await git.reset(['--hard', `origin/${branch}`]);
147+
console.log(`Reset to origin/${branch}`);
148+
}
149+
} catch (resetError) {
150+
console.log(`Warning: Could not reset to origin/${branch}:`, resetError.message);
151+
console.log('Continuing with local branch checkout only');
152+
}
131153
}
132154
} else if (remoteBranchExists) {
133155
await git.checkout(['-b', branch, `origin/${branch}`]);

0 commit comments

Comments
 (0)