-
Notifications
You must be signed in to change notification settings - Fork 0
Fix git push in new worktrees #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -160,6 +160,30 @@ def set_upstream(branch: str, remote: str = "origin", | |||||
| run_git(["branch", f"--set-upstream-to={remote}/{remote_branch}", branch], cwd=path) | ||||||
|
|
||||||
|
|
||||||
| def configure_push_remote(branch: str, remote: str = "origin", | ||||||
| remote_branch: Optional[str] = None, path: Optional[Path] = None): | ||||||
| """ | ||||||
| Configure where a branch should push to, even if remote branch doesn't exist yet. | ||||||
|
|
||||||
| This sets branch.{branch}.remote and branch.{branch}.merge so that 'git push' | ||||||
| will work without needing to specify the remote or use -u flag. | ||||||
|
|
||||||
| Args: | ||||||
| branch: Local branch name | ||||||
| remote: Remote name | ||||||
| remote_branch: Remote branch name (defaults to same as local) | ||||||
|
dev-ankit marked this conversation as resolved.
|
||||||
| path: Repository path to run git commands in | ||||||
| """ | ||||||
| if remote_branch is None: | ||||||
| remote_branch = branch | ||||||
|
|
||||||
| # Set the remote | ||||||
| run_git(["config", f"branch.{branch}.remote", remote], cwd=path) | ||||||
|
|
||||||
| # Set the merge target (what the branch tracks/pushes to) | ||||||
|
||||||
| # Set the merge target (what the branch tracks/pushes to) | |
| # Set the upstream/merge target (used for pulling and to infer push target) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -170,15 +170,25 @@ def create_worktree(self, name: str, base: Optional[str] = None, | |||||
| except git.GitError as e: | ||||||
| raise git.GitError(f"Failed to create worktree: {e}") | ||||||
|
|
||||||
| # Set upstream tracking if not detached | ||||||
| if not detached: | ||||||
| # Configure push remote if not detached | ||||||
| if not detached and create_branch: | ||||||
| try: | ||||||
| # Set upstream to origin/<branch> | ||||||
| remote_branch = branch | ||||||
| git.set_upstream(branch, "origin", remote_branch, self.repo_root) | ||||||
| # For new branches, configure where to push (so git push works without -u) | ||||||
| # This works even if remote branch doesn't exist yet | ||||||
| git.configure_push_remote(branch, "origin", branch, wt_path) | ||||||
|
||||||
| except git.GitError: | ||||||
| # Upstream setting might fail if remote doesn't exist yet | ||||||
| # This is okay, user can push later | ||||||
| # If this fails, user can still push with -u flag | ||||||
| pass | ||||||
|
Comment on lines
+174
to
+181
|
||||||
| elif not detached: | ||||||
| try: | ||||||
| # For existing branches, check if remote branch exists and set upstream | ||||||
| if git.remote_branch_exists(branch, "origin", self.repo_root): | ||||||
| git.set_upstream(branch, "origin", branch, self.repo_root) | ||||||
|
||||||
| git.set_upstream(branch, "origin", branch, self.repo_root) | |
| git.set_upstream(branch, "origin", branch, wt_path) |
Copilot
AI
Jan 18, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with the set_upstream call on line 186, consider using self.repo_root instead of wt_path as the path parameter. While git config commands work from any worktree directory, using a consistent pattern makes the code more maintainable.
| git.configure_push_remote(branch, "origin", branch, wt_path) | |
| git.configure_push_remote(branch, "origin", branch, self.repo_root) |
Copilot
AI
Jan 18, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new push remote configuration behavior for existing branches is not tested. Consider adding a test that verifies the correct configuration is set when creating a worktree with an existing branch that has no remote branch yet. This would ensure both code paths (lines 185-189) are adequately tested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test for
configure_push_remoteonly verifies that the configuration is set correctly but doesn't test the main use case described in the PR: that it works even when the remote branch doesn't exist yet. Consider adding an assertion that attempts to rungit push(or at least simulates it) to verify the configuration allows pushing without the -u flag.