Skip to content

Commit 164cadf

Browse files
dev-ankitclaudeCopilot
authored
Fix git push in new worktrees (#13)
When creating a new worktree with a new branch, git push would fail because no upstream was configured. Users had to manually run 'git push -u origin <branch>' every time. This fix adds a new function `configure_push_remote()` that sets the branch's remote and merge configuration even before the remote branch exists. This allows 'git push' to work automatically without requiring the -u flag. Changes: - Added git.configure_push_remote() to set branch.{branch}.remote and branch.{branch}.merge - Updated worktree creation to use configure_push_remote() for new branches - For existing branches, check if remote exists and use appropriate method - Added test coverage for the new function Closes: Worktree git push configuration issue --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 67e11be commit 164cadf

3 files changed

Lines changed: 57 additions & 7 deletions

File tree

tools/wt-worktree/tests/test_git.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,19 @@ def test_diff_trees(git_repo):
143143
# Get diff
144144
diff = git.diff_trees("HEAD~1", "HEAD", git_repo)
145145
assert "file2.txt" in diff
146+
147+
148+
def test_configure_push_remote(git_repo):
149+
"""Test configuring push remote for a branch."""
150+
# Create a new branch
151+
git.create_branch("test-branch", "HEAD", git_repo)
152+
153+
# Configure push remote
154+
git.configure_push_remote("test-branch", "origin", "test-branch", git_repo)
155+
156+
# Verify configuration was set
157+
result = git.run_git(["config", "branch.test-branch.remote"], cwd=git_repo)
158+
assert result.stdout.strip() == "origin"
159+
160+
result = git.run_git(["config", "branch.test-branch.merge"], cwd=git_repo)
161+
assert result.stdout.strip() == "refs/heads/test-branch"

tools/wt-worktree/wt/git.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ def set_upstream(branch: str, remote: str = "origin",
160160
run_git(["branch", f"--set-upstream-to={remote}/{remote_branch}", branch], cwd=path)
161161

162162

163+
def configure_push_remote(branch: str, remote: str = "origin",
164+
remote_branch: Optional[str] = None, path: Optional[Path] = None):
165+
"""
166+
Configure where a branch should push to, even if remote branch doesn't exist yet.
167+
168+
This sets branch.{branch}.remote and branch.{branch}.merge so that 'git push'
169+
will work without needing to specify the remote or use -u flag.
170+
171+
Args:
172+
branch: Local branch name
173+
remote: Remote name
174+
remote_branch: Remote branch name (defaults to same as local)
175+
path: Repository path to run git commands in
176+
"""
177+
if remote_branch is None:
178+
remote_branch = branch
179+
180+
# Set the remote
181+
run_git(["config", f"branch.{branch}.remote", remote], cwd=path)
182+
183+
# Set the merge target (what the branch tracks/pushes to)
184+
run_git(["config", f"branch.{branch}.merge", f"refs/heads/{remote_branch}"], cwd=path)
185+
186+
163187
def list_worktrees(path: Optional[Path] = None) -> List[dict]:
164188
"""
165189
List all worktrees.

tools/wt-worktree/wt/worktree.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,25 @@ def create_worktree(self, name: str, base: Optional[str] = None,
170170
except git.GitError as e:
171171
raise git.GitError(f"Failed to create worktree: {e}")
172172

173-
# Set upstream tracking if not detached
174-
if not detached:
173+
# Configure push remote if not detached
174+
if not detached and create_branch:
175175
try:
176-
# Set upstream to origin/<branch>
177-
remote_branch = branch
178-
git.set_upstream(branch, "origin", remote_branch, self.repo_root)
176+
# For new branches, configure where to push (so git push works without -u)
177+
# This works even if remote branch doesn't exist yet
178+
git.configure_push_remote(branch, "origin", branch, wt_path)
179179
except git.GitError:
180-
# Upstream setting might fail if remote doesn't exist yet
181-
# This is okay, user can push later
180+
# If this fails, user can still push with -u flag
181+
pass
182+
elif not detached:
183+
try:
184+
# For existing branches, check if remote branch exists and set upstream
185+
if git.remote_branch_exists(branch, "origin", self.repo_root):
186+
git.set_upstream(branch, "origin", branch, self.repo_root)
187+
else:
188+
# Remote doesn't exist yet, configure push target instead
189+
git.configure_push_remote(branch, "origin", branch, wt_path)
190+
except git.GitError:
191+
# If this fails, user can still push with -u flag
182192
pass
183193

184194
return wt_path

0 commit comments

Comments
 (0)