Fix git push in new worktrees#13
Conversation
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
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue where git push fails in newly created worktrees due to missing upstream configuration. The solution introduces a new configure_push_remote() function that sets the branch's push remote and merge configuration before the remote branch exists, allowing git push to work without the -u flag.
Changes:
- Added
git.configure_push_remote()function to configure branch push settings - Updated worktree creation logic to use the new function for both new and existing branches
- Added unit test for the new
configure_push_remote()function
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| tools/wt-worktree/wt/git.py | Adds configure_push_remote() function to set branch remote and merge configuration using git config commands |
| tools/wt-worktree/wt/worktree.py | Updates create_worktree() to configure push remote for new branches and existing branches without remote counterparts |
| tools/wt-worktree/tests/test_git.py | Adds unit test to verify the new function sets correct git config values |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| git.set_upstream(branch, "origin", branch, self.repo_root) | ||
| else: | ||
| # Remote doesn't exist yet, configure push target instead | ||
| git.configure_push_remote(branch, "origin", branch, wt_path) |
There was a problem hiding this comment.
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) |
| 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 |
There was a problem hiding this comment.
The new push remote configuration behavior introduced in create_worktree is not tested. Consider adding a test that verifies the branch.{branch}.remote and branch.{branch}.merge configuration is set correctly after creating a worktree with a new branch. This would ensure the main purpose of this PR (fixing git push in new worktrees) is properly tested at the integration level.
| 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) | ||
| else: | ||
| # Remote doesn't exist yet, configure push target instead | ||
| git.configure_push_remote(branch, "origin", branch, wt_path) | ||
| except git.GitError: | ||
| # If this fails, user can still push with -u flag | ||
| pass |
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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. The same applies to line 189.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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) |
There was a problem hiding this comment.
For consistency with the other calls to configure_push_remote on lines 178 and 189, this should use wt_path instead of self.repo_root. While git branch --set-upstream-to can be run from anywhere in the repository, using the worktree path is more consistent and clearer in intent.
| git.set_upstream(branch, "origin", branch, self.repo_root) | |
| git.set_upstream(branch, "origin", branch, wt_path) |
| def test_configure_push_remote(git_repo): | ||
| """Test configuring push remote for a branch.""" | ||
| # Create a new branch | ||
| git.create_branch("test-branch", "HEAD", git_repo) | ||
|
|
||
| # Configure push remote | ||
| git.configure_push_remote("test-branch", "origin", "test-branch", git_repo) | ||
|
|
||
| # Verify configuration was set | ||
| result = git.run_git(["config", "branch.test-branch.remote"], cwd=git_repo) | ||
| assert result.stdout.strip() == "origin" | ||
|
|
||
| result = git.run_git(["config", "branch.test-branch.merge"], cwd=git_repo) | ||
| assert result.stdout.strip() == "refs/heads/test-branch" |
There was a problem hiding this comment.
The test for configure_push_remote only 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 run git push (or at least simulates it) to verify the configuration allows pushing without the -u flag.
| # Set the remote | ||
| run_git(["config", f"branch.{branch}.remote", remote], cwd=path) | ||
|
|
||
| # Set the merge target (what the branch tracks/pushes to) |
There was a problem hiding this comment.
The comment "Set the merge target (what the branch tracks/pushes to)" is slightly misleading. The branch.{branch}.merge config is primarily used for pulling/fetching, not pushing. While setting both branch.{branch}.remote and branch.{branch}.merge does enable git push to work without flags, it would be more accurate to say "Set the upstream branch for pulling" or "Set the merge target (used for both pulling and to infer push target)".
| # Set the merge target (what the branch tracks/pushes to) | |
| # Set the upstream/merge target (used for pulling and to infer push target) |
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 ' 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:
Closes: Worktree git push configuration issue