Move uncommitted changes to a Git worktree
Move staged edits, unstaged changes, and new files into a separate Git worktree. Keep the original staging intact and verify the transfer before you commit.
Riley Hilliard3 min read
gitagents
You start fixing a bug on the main branch, then realize you need that checkout clean to review someone else’s change. Create a Git worktree at the same commit, stash the edits in the original checkout, and apply the stash in the new one with --index. Even a file with one hunk staged and another still unstaged can keep that split.
Create the destination before moving the edits
Run these commands from the root of the checkout containing your work. Choose an unused branch name and a new sibling directory. The final HEAD puts the new branch at the exact commit underneath your edits, including any local commits you have yet to push.
git status --short --untracked-files=all
git worktree add -b fix/report-date ../report-date HEAD
Keep the status output for comparison. A linked worktree has its own files and staging area, so at this point the original still holds your edits and the new directory is clean. Both checkouts can see the same stash entries. Git’s worktree manual
Stash the tracked changes and new files
This moves the tracked changes and ordinary untracked files out of the original checkout. Read the status list first, especially if another person or agent has also been editing there. If Git lists edits inside a submodule, save them from inside that submodule first. Its files belong to a separate repository.
git stash push --include-untracked -m 'move report-date edits'
git stash list --format='%H %gd %gs'
Find the line with your message and copy its full commit ID, the first field. If Git said there were no local changes to save, stop here. A previous stash can still be at the top of the list. The ID identifies your saved edits even if someone creates another stash before you finish.
--include-untracked covers a new source file you haven’t added yet. Ignored files such as local environment settings and installed dependencies stay in the original directory. If the new checkout needs dependencies, run the project’s usual install there. Git’s stash options
Restore the staging area and check the transfer
Replace STASH_ID below with the ID you copied. The -C option runs each Git command in the new directory while your shell stays in the original one.
git -C ../report-date stash apply --index STASH_ID
git -C ../report-date status --short --untracked-files=all
git -C ../report-date diff --cached
git -C ../report-date diff
Compare the two status columns with the original output. For example, MM report.ts means the file still has both staged and unstaged edits. The cached diff shows what the next commit would include. The plain diff shows the work you left unstaged. New files should appear at the paths you recorded.
Applying a stash keeps the saved copy. If the apply reports a conflict, inspect the destination with git status before doing anything else. A different base commit or an untracked file already occupying a destination path can prevent a clean restore. You can inspect the saved patch, including new files, with git stash show -p --include-untracked STASH_ID.
Run git status --short --untracked-files=all in the original checkout to confirm it’s clean. Then resume work in ../report-date. If you’re handing that directory to an agent, give it its own ports and test database as well.