THE GITHTML FIELD GUIDE

Move uncommitted work to the right Git branch

If uncommitted edits belong on a new branch from the current commit, git switch -c can create that branch while retaining the edits. If they belong on an existing branch with different content, preserve them first and switch carefully. Uncommitted changes are working-tree state, not permanently attached to the branch where you typed them.

Distinguish a new branch from an existing destination

The simplest case is realizing you started a feature while still on main and have not committed. Creating a new branch at the same commit keeps the baseline and gives future commits the right destination. There is no need to commit to main first or undo history that does not yet exist.

An existing destination branch may have different files. Switching can carry compatible edits, but Git stops when it would overwrite them. Do not force the switch. Inspect both staged and unstaged changes, and identify new files that need protection. If some edits belong to another task, preserve that boundary rather than moving the entire checkout indiscriminately.

Use the smallest appropriate transition

The example covers a new branch from the current commit. Confirm the name is unused and that the current base is suitable. If the destination is an existing branch, use a verified scoped stash or checkpoint strategy instead, then inspect the result after switching and reapplying.

  1. Before committing, check the branch name again and review the staged diff. Working files can look identical even when HEAD is on the wrong branch.
  2. If using stash to cross to a different base, apply rather than immediately discard the entry, and verify any untracked files were included when needed.
shell
git status --short --branch
git diff
git diff --staged
git switch -c feature/search-filter
git status --short --branch

Confirm intent rather than just a successful switch

Run the relevant tests after applying edits to a different base. The destination may have changed interfaces or assumptions even if the patch applies cleanly. Keep the saved copy until the transferred work is verified, then remove only the specific temporary checkpoint when appropriate.

If you discover that a commit was already made, stop using the uncommitted-work recipe and inspect that commit’s publication status. Moving committed work requires a separate history decision. For this case, the desired result is straightforward: the original edits remain intact, unrelated work remains separate, and the next commit advances the correct branch. An empty status display is not required to achieve that result.

  1. If main was already behind the project when you started editing, creating a feature branch preserves that same older base. It solves the naming mistake but does not incorporate newer upstream changes. Decide separately whether the feature needs an updated base before submitting it for review.
  2. For a mixture of unrelated edits, list the paths or hunks belonging to the intended feature before moving them. A branch switch carries working state, not your mental task boundaries. Review the eventual staged snapshot against that list so another unfinished task does not enter the feature commit.

Sources and further reading

AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.

All fixing git problems guides →

Read as MarkdownAll guides

Move uncommitted work to the right Git branch · gitHtml