Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix: unset unsafe upstream when creating branch from remote base (#717)

Maximilian Roosfd2ae0e88267
diff --git a/.claude-plugin/skills/worktrunk/reference/switch.md b/.claude-plugin/skills/worktrunk/reference/switch.md
index 8f7be0c6c..8312b9626 100644
--- a/.claude-plugin/skills/worktrunk/reference/switch.md
+++ b/.claude-plugin/skills/worktrunk/reference/switch.md
@@ -18,6 +18,10 @@ wt switch pr:123 # Switch to PR #123's branch
The `--create` flag creates a new branch from the `--base` branch (defaults to default branch). Without `--create`, the branch must already exist.
+**Upstream tracking:** Branches created with `--create` have no upstream tracking configured. This prevents accidental pushes to the wrong branch — for example, `--base origin/main` would otherwise make `git push` target `main`. Use `git push -u origin <branch>` to set up tracking when you're ready.
+
+Without `--create`, switching to a remote branch (e.g., `wt switch feature` when only `origin/feature` exists) creates a local branch tracking the remote — this is the standard git behavior and is preserved.
+
## Creating worktrees
If the branch already has a worktree, `wt switch` changes directories to it. Otherwise, it creates one, running [hooks](https://worktrunk.dev/hook/).
diff --git a/docs/content/switch.md b/docs/content/switch.md
index 66df3d68d..c2a253835 100644
--- a/docs/content/switch.md
+++ b/docs/content/switch.md
@@ -32,6 +32,10 @@ wt switch pr:123 # Switch to PR #123's branch
The `--create` flag creates a new branch from the `--base` branch (defaults to default branch). Without `--create`, the branch must already exist.
+**Upstream tracking:** Branches created with `--create` have no upstream tracking configured. This prevents accidental pushes to the wrong branch — for example, `--base origin/main` would otherwise make `git push` target `main`. Use `git push -u origin <branch>` to set up tracking when you're ready.
+
+Without `--create`, switching to a remote branch (e.g., `wt switch feature` when only `origin/feature` exists) creates a local branch tracking the remote — this is the standard git behavior and is preserved.
+
## Creating worktrees
If the branch already has a worktree, `wt switch` changes directories to it. Otherwise, it creates one, running [hooks](@/hook.md).
diff --git a/src/cli/mod.rs b/src/cli/mod.rs
index 0ad93b2a0..0ffe810d4 100644
--- a/src/cli/mod.rs
+++ b/src/cli/mod.rs
@@ -263,6 +263,10 @@ wt switch pr:123 # Switch to PR #123's branch
The `--create` flag creates a new branch from the `--base` branch (defaults to default branch). Without `--create`, the branch must already exist.
+**Upstream tracking:** Branches created with `--create` have no upstream tracking configured. This prevents accidental pushes to the wrong branch — for example, `--base origin/main` would otherwise make `git push` target `main`. Use `git push -u origin <branch>` to set up tracking when you're ready.
+
+Without `--create`, switching to a remote branch (e.g., `wt switch feature` when only `origin/feature` exists) creates a local branch tracking the remote — this is the standard git behavior and is preserved.
+
## Creating worktrees
If the branch already has a worktree, `wt switch` changes directories to it. Otherwise, it creates one, running [hooks](@/hook.md).
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rs
index 6b9158876..e71cb2bba 100644
--- a/src/commands/worktree/switch.rs
+++ b/src/commands/worktree/switch.rs
@@ -476,6 +476,19 @@ pub fn execute_switch(
.into());
}
+ // Safety: unset unsafe upstream when creating a new branch from a remote
+ // tracking branch. When `git worktree add -b feature origin/main` runs,
+ // git sets feature to track origin/main. This is dangerous because
+ // `git push` would push to main instead of the feature branch.
+ // See: https://github.com/max-sixty/worktrunk/issues/713
+ if *create_branch
+ && let Some(base) = base_branch
+ && repo.is_remote_tracking_branch(base)
+ {
+ // Unset the upstream to prevent accidental pushes
+ branch_handle.unset_upstream()?;
+ }
+
// Report tracking info only if git's DWIM created the branch from a remote
let from_remote = if !create_branch && !local_branch_existed {
branch_handle.upstream()?
diff --git a/src/git/repository/branch.rs b/src/git/repository/branch.rs
index 01e1370e4..50b629477 100644
--- a/src/git/repository/branch.rs
+++ b/src/git/repository/branch.rs
@@ -115,6 +115,16 @@ impl<'a> Branch<'a> {
}
}
+ /// Unset the upstream tracking branch for this branch.
+ ///
+ /// This removes the tracking relationship, preventing accidental pushes
+ /// to the wrong branch (e.g., when a feature branch was created from origin/main).
+ pub fn unset_upstream(&self) -> anyhow::Result<()> {
+ self.repo
+ .run_command(&["branch", "--unset-upstream", &self.name])?;
+ Ok(())
+ }
+
/// Get the remote where this branch would be pushed.
///
/// Uses [`@{push}` syntax][1] which resolves through:
diff --git a/src/git/repository/remotes.rs b/src/git/repository/remotes.rs
index 49897cc3f..9d9e54699 100644
--- a/src/git/repository/remotes.rs
+++ b/src/git/repository/remotes.rs
@@ -162,4 +162,17 @@ impl Repository {
.and_then(|config| config.list)
.and_then(|list| list.url)
}
+
+ /// Check if a ref is a remote tracking branch.
+ ///
+ /// Returns true if the ref exists under `refs/remotes/` (e.g., `origin/main`).
+ /// Returns false for local branches, tags, SHAs, and non-existent refs.
+ pub fn is_remote_tracking_branch(&self, ref_name: &str) -> bool {
+ self.run_command(&[
+ "rev-parse",
+ "--verify",
+ &format!("refs/remotes/{}", ref_name),
+ ])
+ .is_ok()
+ }
}
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index 382f9cc36..268d5c382 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -106,6 +106,41 @@ fn test_switch_dwim_from_remote(#[from(repo_with_remote)] repo: TestRepo) {
snapshot_switch("switch_dwim_from_remote", &repo, &["dwim-feature"]);
}
+/// When creating a new branch from a remote tracking branch (e.g., origin/main),
+/// the new branch should NOT track the remote base branch.
+/// This prevents accidental `git push` to the base branch (e.g., pushing to main).
+/// This is the bug fix for GitHub issue #713.
+#[rstest]
+fn test_switch_create_from_remote_base_no_upstream(#[from(repo_with_remote)] repo: TestRepo) {
+ // Create a new branch with --base pointing to a remote tracking branch
+ let output = repo
+ .wt_command()
+ .args(["switch", "--create", "my-feature", "--base=origin/main"])
+ .output()
+ .unwrap();
+ assert!(output.status.success(), "switch should succeed");
+
+ // Verify the branch was created
+ let branch_output = repo.git_output(&["branch", "--list", "my-feature"]);
+ assert!(
+ branch_output.contains("my-feature"),
+ "branch should be created"
+ );
+
+ // Verify the branch does NOT have an upstream (no tracking)
+ // Using rev-parse to check for upstream - should fail for untracked branches
+ let upstream_check = repo
+ .git_command()
+ .args(["rev-parse", "--abbrev-ref", "my-feature@{upstream}"])
+ .output()
+ .unwrap();
+
+ assert!(
+ !upstream_check.status.success(),
+ "branch should NOT have upstream tracking (to prevent accidental push to origin/main)"
+ );
+}
+
/// When local branch already exists and tracks a remote, should report
/// "Created worktree for X" NOT "Created branch X (tracking remote)".
/// This is the bug fix for GitHub issue #656.
diff --git a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
index 6f515ef10..d8c7b370d 100644
--- a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
+++ b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
@@ -8,6 +8,7 @@ info:
env:
CLICOLOR_FORCE: "1"
COLUMNS: "500"
+ GIT_EDITOR: ""
RUST_LOG: warn
SHELL: ""
SOURCE_DATE_EPOCH: "1735776000"
@@ -98,6 +99,10 @@ Worktrees are addressed by branch name; paths are computed from a configurable t
The --create flag creates a new branch from the --base branch (defaults to default branch). Without --create, the branch must already exist.
+Upstream tracking: Branches created with --create have no upstream tracking configured. This prevents accidental pushes to the wrong branch — for example, --base origin/main would otherwise make git push target main. Use git push -u origin <branch> to set up tracking when you're ready.
+
+Without --create, switching to a remote branch (e.g., wt switch feature when only origin/feature exists) creates a local branch tracking the remote — this is the standard git behavior and is preserved.
+
Creating worktrees
If the branch already has a worktree, wt switch changes directories to it. Otherwise, it creates one, running hooks.