Skip to content

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

feat(merge): Add --tracked-only flag and warn for untracked

Maximilian Roosea2e3736647b
diff --git a/README.md b/README.md
index 8a5d7acc5..78f6c0de5 100644
--- a/README.md
+++ b/README.md
@@ -14,7 +14,7 @@ Running ten agents on different features? `wt switch --create feature-a`, `wt sw
## What It Does
-Automates the full lifecycle: create worktree, work, merge back to local main, remove worktree.
+Automates the full lifecycle: create worktree, work, merge back, remove worktree.
```bash
$ wt switch --create fix-auth
@@ -22,21 +22,15 @@ $ wt switch --create fix-auth
# Agent works, makes changes, then:
$ wt merge main --squash
-πŸ”„ Rebasing onto main...
-πŸ”„ Merging 1 commit to main @ a1b2c3d
-
-* a1b2c3d (HEAD -> fix-auth) Add authentication
-
- auth.js | 45 +++++++++++++++++++++++++++++++++++++++++++++
- 1 file changed, 45 insertions(+)
-
-βœ… Merged to main (1 commit, 1 file, +45)
-πŸ”„ Cleaning up worktree...
-βœ… Returned to primary at ../repo
+πŸ”„ Switching to main...
+πŸ”„ Staging changes (3 modified)...
+πŸ”„ Merging fix-auth β†’ main...
+βœ… Merged and pushed
+βœ… Removed worktree
# Shell back in main
```
-Shell integration means directories change automatically. Merge handles staging, committing, merging to main, cleanup. One command.
+Shell integration means directories change automatically. Merge handles staging, committing, merging, pushing, cleanup. One command.
## Installation
@@ -54,9 +48,8 @@ wt switch --create feature-name
**Finish and merge:**
```bash
-wt merge main --squash # Rebases onto main, merges to main, removes worktree
+wt merge main --squash
```
-Merges your changes into main: rebases your branch onto the latest main, merges the result into your local main branch, and cleans up the worktree. Use `--squash` to combine all commits into one.
**See active worktrees:**
```bash
@@ -84,39 +77,20 @@ wt config help # Setup guide
Worktrunk is opinionated. The choices optimize for AI agent workflows:
-1. **Merge does everything** - Staging, committing, merging to main, cleanup in one command
+1. **Merge does everything** - Staging, committing all changes, merging, pushing, cleanup in one command
2. **Squash by default** - Linear history, configurable
3. **Automatic shell navigation** - No manual `cd` commands
4. **Fail-fast hooks** - Tests block bad merges
These trade manual control for automation. For fine-grained control, use `git worktree` directly.
-## Local Workflow Model
-
-Worktrunk manages **local git worktrees** with **local operations**:
-
-- **Local operations only** - `wt merge` merges commits to your local main branch, not to remote repositories
-- **Fast-forward only** - No force pushes or commit rewriting; main branch advances cleanly forward
-- **Remote sync separate** - You manage `git push`/`git pull` to remotes yourself, worktrunk focuses on local branch workflow
-
-**Example workflow:**
-```bash
-wt switch --create feature # Create feature worktree
-# ... make changes ...
-wt merge main # Rebase and merge to local main branch
-cd ../repo # Switch to main worktree
-git push origin main # Push to remote (standard git)
-```
-
-Worktrunk doesn't replace gitβ€”it automates the worktree lifecycle on top of it.
-
## All Commands
- `wt switch [branch]` - Switch to existing worktree
- `wt switch --create [branch]` - Create and switch
- `wt remove` - Remove current, return to main
-- `wt merge [target]` - Rebase onto target, merge to target, and cleanup worktree
-- `wt push [target]` - Fast-forward merge to another branch (no rebase)
+- `wt merge [target]` - Merge, push, cleanup
+- `wt push [target]` - Move changes to another branch
- `wt list` - Show all worktrees
- `wt config` - Manage configuration
@@ -293,7 +267,7 @@ Automate common tasks by creating `.config/wt.toml` in your repository root. Run
| **pre-commit-command** | Before committing changes during `wt merge` (when not squashing) | Sequential, blocking, fail-fast | Terminates merge immediately |
| **pre-squash-command** | Before squashing commits during `wt merge --squash` | Sequential, blocking, fail-fast | Terminates merge immediately |
| **pre-merge-command** | Before any commits/rebasing during `wt merge` | Sequential, blocking, fail-fast | Terminates merge immediately |
-| **post-merge-command** | After successful merge to target branch, before cleanup | Sequential, blocking | Logs warning, continues with remaining commands |
+| **post-merge-command** | After successful merge and push to target branch, before cleanup | Sequential, blocking | Logs warning, continues with remaining commands |
**Template variables:** `{repo}`, `{branch}`, `{worktree}`, `{repo_root}`, `{target}`
@@ -325,14 +299,14 @@ worktree-path = "../worktrees/{main-worktree}/{branch}"
### Fast Branch Switching
-Move changes from the current worktree directly to another branch without committing or merging. Useful for moving work-in-progress code.
+Push changes from the current worktree directly to another branch without committing or merging. Useful for moving work-in-progress code.
```bash
-# Move current changes to another branch
+# Push current changes to another branch
$ wt push feature-experiment
```
-Worktrunk stages the changes, creates a commit, and merges it to the target branch.
+Worktrunk stages the changes, creates a commit, and pushes it to the target branch's worktree if it exists.
### Shell Integration Details
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index d70867684..064226bd0 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -1,19 +1,57 @@
use worktrunk::config::{ProjectConfig, WorktrunkConfig};
use worktrunk::git::{GitError, GitResultExt, Repository};
use worktrunk::styling::{
- AnstyleStyle, CYAN, CYAN_BOLD, HINT, HINT_EMOJI, format_bash_with_gutter, format_with_gutter,
+ AnstyleStyle, CYAN, CYAN_BOLD, HINT, HINT_EMOJI, WARNING, WARNING_EMOJI,
+ format_bash_with_gutter, format_with_gutter,
};
use super::command_executor::{CommandContext, prepare_project_commands};
use super::worktree::{handle_push, handle_remove, parse_diff_shortstat};
use crate::output::execute_command_in_worktree;
+/// Extract untracked files from git status --porcelain output
+fn get_untracked_files(status_output: &str) -> Vec<String> {
+ let mut untracked = Vec::new();
+
+ for line in status_output.lines() {
+ // Git status --porcelain format: XY filename
+ // Untracked files have "??" status
+ if let Some(filename) = line.strip_prefix("?? ") {
+ untracked.push(filename.to_string());
+ }
+ }
+
+ untracked
+}
+
+/// Warn about untracked files being auto-staged
+fn show_untracked_warning(repo: &Repository) -> Result<(), GitError> {
+ let status = repo
+ .run_command(&["status", "--porcelain"])
+ .git_context("Failed to get status")?;
+ let untracked = get_untracked_files(&status);
+
+ if untracked.is_empty() {
+ return Ok(());
+ }
+
+ // Format file list (comma-separated)
+ let file_list = untracked.join(", ");
+
+ crate::output::progress(format!(
+ "{WARNING_EMOJI} {WARNING}Auto-staging untracked files: {file_list}{WARNING:#}"
+ ))?;
+
+ Ok(())
+}
+
pub fn handle_merge(
target: Option<&str>,
squash_enabled: bool,
keep: bool,
no_hooks: bool,
force: bool,
+ tracked_only: bool,
) -> Result<(), GitError> {
let repo = Repository::current();
@@ -39,12 +77,27 @@ pub fn handle_merge(
// Handle uncommitted changes depending on whether we're squashing
if repo.is_dirty()? {
if squash_enabled {
- // Just stage - squash will handle committing
- repo.run_command(&["add", "-A"])
- .git_context("Failed to stage changes")?;
+ // Warn about untracked files before staging
+ if !tracked_only {
+ show_untracked_warning(&repo)?;
+ }
+
+ if tracked_only {
+ repo.run_command(&["add", "-u"])
+ .git_context("Failed to stage tracked changes")?;
+ } else {
+ repo.run_command(&["add", "-A"])
+ .git_context("Failed to stage changes")?;
+ }
} else {
// Commit immediately when not squashing
- handle_commit_changes(&config.commit_generation, &current_branch, no_hooks, force)?;
+ handle_commit_changes(
+ &config.commit_generation,
+ &current_branch,
+ no_hooks,
+ force,
+ tracked_only,
+ )?;
}
}
@@ -291,6 +344,7 @@ fn handle_commit_changes(
current_branch: &str,
no_hooks: bool,
force: bool,
+ tracked_only: bool,
) -> Result<(), GitError> {
let repo = Repository::current();
let config = WorktrunkConfig::load().git_context("Failed to load config")?;
@@ -309,9 +363,19 @@ fn handle_commit_changes(
)?;
}
- // Stage all changes including untracked files
- repo.run_command(&["add", "-A"])
- .git_context("Failed to stage changes")?;
+ // Warn about untracked files before staging (only if using git add -A)
+ if !tracked_only {
+ show_untracked_warning(&repo)?;
+ }
+
+ // Stage changes
+ if tracked_only {
+ repo.run_command(&["add", "-u"])
+ .git_context("Failed to stage tracked changes")?;
+ } else {
+ repo.run_command(&["add", "-A"])
+ .git_context("Failed to stage changes")?;
+ }
commit_with_generated_message("Committing changes...", commit_generation_config)
}
diff --git a/src/main.rs b/src/main.rs
index b9fabf860..98d3b2882 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -255,6 +255,10 @@ Skip pre-merge commands:
/// Skip approval prompts for commands
#[arg(short, long)]
force: bool,
+
+ /// Only stage tracked files (git add -u) instead of all files (git add -A)
+ #[arg(long)]
+ tracked_only: bool,
},
/// Generate shell completion script (deprecated - use init instead)
@@ -509,7 +513,15 @@ fn main() {
keep,
no_hooks,
force,
- } => handle_merge(target.as_deref(), squash_enabled, keep, no_hooks, force),
+ tracked_only,
+ } => handle_merge(
+ target.as_deref(),
+ squash_enabled,
+ keep,
+ no_hooks,
+ force,
+ tracked_only,
+ ),
Commands::Completion { shell } => {
let mut cli_cmd = Cli::command();
handle_completion(shell, &mut cli_cmd);
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index 1baf34546..23c211801 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -880,6 +880,56 @@ fn test_merge_auto_commit_and_squash() {
);
}
+#[test]
+fn test_merge_with_untracked_files() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ // Create a worktree for main
+ let main_wt = repo.root_path().parent().unwrap().join("test-repo.main-wt");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["worktree", "add", main_wt.to_str().unwrap(), "main"])
+ .current_dir(repo.root_path())
+ .output()
+ .expect("Failed to add worktree");
+
+ // Create a feature worktree with one commit
+ let feature_wt = repo.add_worktree("feature", "feature");
+ std::fs::write(feature_wt.join("file1.txt"), "content 1").expect("Failed to write file");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "file1.txt"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to add file");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "feat: add file 1"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to commit");
+
+ // Add untracked files
+ std::fs::write(feature_wt.join("untracked1.txt"), "untracked content 1")
+ .expect("Failed to write file");
+ std::fs::write(feature_wt.join("untracked2.txt"), "untracked content 2")
+ .expect("Failed to write file");
+
+ // Merge - should show warning about untracked files
+ snapshot_merge_with_env(
+ "merge_with_untracked_files",
+ &repo,
+ &["main"],
+ Some(&feature_wt),
+ &[
+ ("WORKTRUNK_COMMIT_GENERATION__COMMAND", "echo"),
+ ("WORKTRUNK_COMMIT_GENERATION__ARGS", "fix: commit changes"),
+ ],
+ );
+}
+
#[test]
fn test_merge_pre_merge_command_success() {
let mut repo = TestRepo::new();
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index 2ab913d4d..38783f861 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpsmlUHy/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmptSlwtE/test-config.toml
---
success: true
exit_code: 0
@@ -402,7 +402,7 @@ _wt() {
return 0
;;
wt__merge)
- opts="-k -f -v -h --no-squash --keep --no-hooks --force --verbose --help [TARGET]"
+ opts="-k -f -v -h --no-squash --keep --no-hooks --force --tracked-only --verbose --help [TARGET]"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index 02f59870e..dd7ab1751 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpDaJquk/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpEtNUfC/test-config.toml
---
success: true
exit_code: 0
@@ -271,6 +271,7 @@ complete -c wt -n "__fish_wt_using_subcommand merge" -l no-squash -d 'Disable sq
complete -c wt -n "__fish_wt_using_subcommand merge" -s k -l keep -d 'Keep worktree after merging (don\'t remove)'
complete -c wt -n "__fish_wt_using_subcommand merge" -l no-hooks -d 'Skip all project hooks (pre-merge-command)'
complete -c wt -n "__fish_wt_using_subcommand merge" -s f -l force -d 'Skip approval prompts for commands'
+complete -c wt -n "__fish_wt_using_subcommand merge" -l tracked-only -d 'Only stage tracked files (git add -u) instead of all files (git add -A)'
complete -c wt -n "__fish_wt_using_subcommand merge" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
complete -c wt -n "__fish_wt_using_subcommand merge" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c wt -n "__fish_wt_using_subcommand completion" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index 68dceb336..70367a81a 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmplCNLKR/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpG3qBik/test-config.toml
---
success: true
exit_code: 0
@@ -312,6 +312,7 @@ _arguments "${_arguments_options[@]}" : \
'--no-hooks[Skip all project hooks (pre-merge-command)]' \
'-f[Skip approval prompts for commands]' \
'--force[Skip approval prompts for commands]' \
+'--tracked-only[Only stage tracked files (git add -u) instead of all files (git add -A)]' \
'-v[Enable verbose output (show git commands and debug info)]' \
'--verbose[Enable verbose output (show git commands and debug info)]' \
'-h[Print help (see more with '\''--help'\'')]' \
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap b/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap
index 134bbe24e..2f9b324b8 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap
@@ -16,11 +16,12 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpc0fetn/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpB0fxlV/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
+🟑 Auto-staging untracked files: dirty.txt
πŸ”„ Committing changes... (1 file, +1)
πŸ”„ Generating commit message...
πŸ’‘ Using fallback commit message. Run 'wt config help' to configure LLM-generated messages
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snap
index d6760b558..201c21bf4 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snap
@@ -17,11 +17,12 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpB1r44t/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpDPSCsN/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
+🟑 Auto-staging untracked files: feature.txt
πŸ”„ Committing changes... (1 file, +1)
πŸ”„ Generating commit message...
πŸ’‘ Using fallback commit message. Run 'wt config help' to configure LLM-generated messages
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap
index bd7ac10ce..3e1d6f00a 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap
@@ -17,11 +17,12 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp6V1hsj/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpboobtT/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
+🟑 Auto-staging untracked files: feature.txt
πŸ”„ Committing changes... (1 file, +1)
πŸ”„ Generating commit message...
πŸ’‘ Using fallback commit message. Run 'wt config help' to configure LLM-generated messages
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap b/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap
new file mode 100644
index 000000000..326095b95
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap
@@ -0,0 +1,51 @@
+---
+source: tests/integration_tests/merge.rs
+info:
+ program: wt
+ args:
+ - merge
+ - main
+ env:
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: /dev/null
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ LANG: C
+ LC_ALL: C
+ SOURCE_DATE_EPOCH: "1761609600"
+ WORKTRUNK_COMMIT_GENERATION__ARGS: "fix: commit changes"
+ WORKTRUNK_COMMIT_GENERATION__COMMAND: echo
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpf9g0jQ/test-config.toml
+---
+success: true
+exit_code: 0
+----- stdout -----
+🟑 Auto-staging untracked files: untracked1.txt, untracked2.txt
+πŸ”„ Squashing 1 commits into 1 (1 file, +1)...
+πŸ”„ Generating squash commit message...
+πŸ’‘ Using fallback commit message. Run 'wt config help' to configure LLM-generated messages
+  Squash commits from main
+ 
+  Combined commits:
+  - feat: add file 1
+
+βœ… Squashed 1 commits into 1 @ [SHA]
+πŸ”„ Rebasing onto main...
+πŸ”„ Merging 1 commit to main @ [SHA]
+
+* [SHA] (HEAD -> feature) Squash commits from main
+
+ file1.txt | 1 +
+ untracked1.txt | 1 +
+ untracked2.txt | 1 +
+ 3 files changed, 3 insertions(+)
+
+βœ… Merged to main (1 commit, 3 files, +3) 
+πŸ”„ Cleaning up worktree...
+
+βœ… Returned to primary at [REPO]
+
+----- stderr -----