fix: use explicit refspec for push in worktrees with slashed branch names#1684
Conversation
…orktrees
When pushing a branch that has no upstream tracking configured (the
`pushWithUpstream` path in `pushCurrentBranch`), git is invoked as:
git push -u <remote> <branch>
This fails in git worktrees when the branch name contains a slash
(e.g. `t3code/my-feature`), with:
fatal: <branch> cannot be resolved to branch
This is a known git limitation — bare branch names with slashes
cannot be resolved from a linked worktree's context during push.
The branch ref exists (verified via `git rev-parse`), and
`git branch --show-current` reports it correctly, but `git push`
fails to resolve it.
The fix uses an explicit refspec instead:
git push -u <remote> HEAD:refs/heads/<branch>
This is semantically equivalent but bypasses the branch name
resolution issue by using HEAD (always valid) as the source ref
and the fully qualified target ref path.
The `pushWithUpstream` code path is only reached when the branch
has no upstream configured — branches that already track a remote
use the simpler `git push` without a branch argument, which is
unaffected by this issue.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
ApprovabilityVerdict: Approved Simple one-line bug fix that uses the explicit git refspec format ( You can customize Macroscope's approvability policy. Learn more. |
…ames (pingdotgg#1684) Co-authored-by: Julius Marminge <julius0216@outlook.com>
…ames (pingdotgg#1684) Co-authored-by: Julius Marminge <julius0216@outlook.com>
Summary
git push -u <remote> <branch>failing in git worktrees when the branch name contains a slash (e.g.t3code/my-feature)Problem
When a worktree branch has no upstream tracking configured,
pushCurrentBranchtakes thepushWithUpstreampath and runs:This fails with:
The branch ref exists —
git rev-parse --verify t3code/my-featureandgit branch --show-currentboth work correctly. Butgit pushwith a bare branch name containing slashes cannot resolve it from a linked worktree's context. This is a known git limitation with worktrees and hierarchical branch names.Reproduction
git worktree add -b t3code/abc12345 /tmp/wt maincd /tmp/wt && git push -u origin t3code/abc12345fatal: t3code/abc12345 cannot be resolved to branchThis affects the default worktree flow since
t3code/<hex>branches are created without upstream tracking, and after the branch rename tot3code/<descriptive-name>, the first push goes through this code path.Fix
Use an explicit refspec instead of a bare branch name:
This is semantically equivalent but bypasses the resolution issue by using
HEAD(always valid in any worktree) as the source and the fully qualified ref path as the target.Only the
pushWithUpstreampath is affected — branches that already track a remote usegit pushwithout a branch argument, which works fine.Test plan
t3code/abc12345→t3code/fix-something)Note
Fix
pushCurrentBranchto use explicit refspec for branches with slashes in their namesIn GitCore.ts, the
git push -u <remote>command now usesHEAD:refs/heads/<branch>instead of<branch>as the ref argument. Branch names containing slashes (e.g.feature/foo) were previously misinterpreted as remote paths by Git, causing the push to fail.Macroscope summarized b4f93eb.
Note
Medium Risk
Changes the initial
git push -uinvocation for branches without an upstream, which could affect how new branches are published (though intended to be equivalent). Main risk is unexpected push behavior in edge cases due to altered refspec handling.Overview
Fixes pushing a branch with no upstream from linked worktrees when the branch name contains slashes.
pushCurrentBranchnow pushes using an explicit refspec (HEAD:refs/heads/<branch>) instead of passing the bare branch name togit push -u, avoiding Git’s branch-resolution issues in worktree contexts.Written by Cursor Bugbot for commit b4f93eb. This will update automatically on new commits. Configure here.