Skip to content

Fix git push in new worktrees#13

Merged
dev-ankit merged 2 commits into
mainfrom
claude/fix-worktree-git-push-is2fW
Jan 18, 2026
Merged

Fix git push in new worktrees#13
dev-ankit merged 2 commits into
mainfrom
claude/fix-worktree-git-push-is2fW

Conversation

@dev-ankit

Copy link
Copy Markdown
Owner

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:

  • 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

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
Copilot AI review requested due to automatic review settings January 18, 2026 14:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copilot AI Jan 18, 2026

Copy link

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.

Suggested change
git.configure_push_remote(branch, "origin", branch, wt_path)
git.configure_push_remote(branch, "origin", branch, self.repo_root)

Copilot uses AI. Check for mistakes.
Comment on lines +174 to +181
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

Copilot AI Jan 18, 2026

Copy link

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 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.

Copilot uses AI. Check for mistakes.
Comment on lines +182 to 192
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

Copilot AI Jan 18, 2026

Copy link

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.

Copilot uses AI. Check for mistakes.
Comment thread tools/wt-worktree/wt/git.py
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)

Copilot AI Jan 18, 2026

Copy link

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. The same applies to line 189.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings January 18, 2026 14:42
@dev-ankit dev-ankit merged commit 164cadf into main Jan 18, 2026
7 checks passed
@dev-ankit dev-ankit deleted the claude/fix-worktree-git-push-is2fW branch January 18, 2026 14:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copilot AI Jan 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
git.set_upstream(branch, "origin", branch, self.repo_root)
git.set_upstream(branch, "origin", branch, wt_path)

Copilot uses AI. Check for mistakes.
Comment on lines +148 to +161
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"

Copilot AI Jan 18, 2026

Copy link

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_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.

Copilot uses AI. Check for mistakes.
# Set the remote
run_git(["config", f"branch.{branch}.remote", remote], cwd=path)

# Set the merge target (what the branch tracks/pushes to)

Copilot AI Jan 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)".

Suggested change
# Set the merge target (what the branch tracks/pushes to)
# Set the upstream/merge target (used for pulling and to infer push target)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants