Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I create and switch to a local Git branch without losing work?

Quick answer

Run git status before switching so you can see whether you have edits to preserve. To create a local branch from your current commit and switch to it in one step, run git switch -c <branch-name>. Git normally stops if switching would overwrite local changes. If your work is unfinished, commit or stash it first; do not use --discard-changes when you want to keep it. A branch name points to a commit, and creating one alone does not switch branches. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What does it mean to create a local branch, and does that move my current work?

Geez

A local branch is a name pointing to a commit. If you create one without specifying another starting point, it points to the commit your current HEAD identifies. Creating the branch alone does not switch you to it. [S2]

For the usual create-and-switch task, use:

git switch -c <branch-name>

That creates the branch at your current HEAD and switches to it. [S1] [S2] This does not mean your uncommitted edits have become a commit: branch names track commits, while your working-tree edits are a separate concern. Check and preserve those edits before switching.

Follow-up question

What should I check before running that command if I already changed files?

Geez

First run:

git status

Review the result and decide whether each listed change is work you need to keep. The Git switch documentation recommends checking the repository and inspecting or preserving changes before switching. [S1] If you are unsure what a listed file contains, inspect it before proceeding; do not treat a branch switch as a backup.

For example, suppose you have edited draft.txt and want a branch called try-new-copy. If the edits are ready to record, commit them first. Then create and switch to the new branch:

git switch -c try-new-copy

This starts the branch at the current commit. [S1] [S2] The name try-new-copy is only an example; replace it with a meaningful name for your task.

Follow-up question

What if my edits are unfinished, or Git refuses to switch because they might be overwritten?

Geez

Treat the refusal as a signal to preserve the changes before trying again. Git normally aborts a switch if updating the target branch would lose local changes. [S1] You can commit work that is ready to record, or stash work you want to set aside temporarily; both are preservation options described in the Git switch documentation. [S1]

After you have preserved the work, run git status again, then retry git switch -c <branch-name>. The status check helps you confirm what remains in the working tree; it does not prove that every important file or idea has been saved elsewhere. If you are not sure what your chosen commit or stash includes, inspect it using your usual Git workflow before switching.

Follow-up question

Can I force the switch, or carry my edits across to the other branch?

Geez

Avoid git switch --discard-changes when you mean to keep your work: that option is specifically not the preservation route. [S1] The documented --merge option attempts to carry local modifications across, but Git may leave conflicts if it cannot reapply them cleanly. [S1] For a straightforward, lower-surprise workflow, preserve the work first, switch, then deal with the saved work deliberately.

A reusable sequence is:

  1. Run git status and review the listed changes.
  2. Commit changes you are ready to record, or stash unfinished changes.
  3. Run git status again and check that you have handled the work you meant to preserve.
  4. Run git switch -c <branch-name> to create and switch to the branch.
  5. Confirm the branch and working-tree state before continuing.

These checks reduce the chance of an accidental loss, but they are not a substitute for a separate backup when a file is especially important.

A practical checklist

  • Run git status before switching and review the changes it lists.

  • Decide which edits are ready to commit and which should be stashed.

  • Preserve unfinished or important work before retrying a blocked switch.

  • Use git switch -c <branch-name> to create a branch from the current commit and switch to it.

  • Avoid --discard-changes when you want to keep local edits.

A few more useful answers

Does `git branch <name>` switch me to the new branch?

No. It creates a branch pointing at the current HEAD by default, but does not switch the current branch. Use git switch <name> afterward, or use git switch -c <name> to create and switch in one step. [S2]

What does `git switch --merge` do with local edits?

It attempts to carry local modifications across to the target branch. If reapplying them conflicts, Git may leave conflicts to resolve; it is not a guarantee that every switch will be clean. [S1]

Will creating or switching to a branch back up my files?

No. A branch is a reference to a commit, not a separate backup of uncommitted edits. Check git status and preserve changes you need before switching. [S1] [S2]

Sources

References used for this guide.

  1. [S1] Git - git-switch Documentation
    git-scm.com · Checked
  2. [S2] Git - git-branch Documentation
    git-scm.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work