Programming

git switch branch without discarding local changes

25 September 2026 · 8 min read

git switch branch without discarding local changes

Switching branches in Git while retaining your uncommitted local changes can be a tricky maneuver. Many developers find themselves stuck, fearing data loss, or resorting to clunky workarounds. However, Git offers elegant solutions for this common scenario, empowering you to navigate your project’s branches with confidence and flexibility. This guide will explore the various methods to switch Git branches without discarding local changes, ensuring a smooth and efficient workflow.

Understanding the Challenge of Switching Branches with Local Changes

Before diving into solutions, it’s crucial to grasp why Git sometimes prevents branch switching when you have uncommitted changes. Git operates by applying changes to specific branches. If you attempt to switch to a different branch while your current branch has uncommitted modifications, Git might refuse to proceed. This safeguard prevents potential conflicts and data loss if the same files have been modified on both branches. Imagine switching to a new branch where the same file you’ve modified locally exists in a different state. Merging these changes automatically could lead to unexpected results or even overwrite your local work.

However, Git provides several ways to manage these situations without losing your progress. Understanding these methods empowers you to work seamlessly across branches without the constant worry of losing your local modifications. This ensures a smoother, more efficient development process.

Using git stash to Temporarily Store Changes

git stash is your go-to command for temporarily shelving your uncommitted changes. Think of it as a safe holding area for your work-in-progress. This command takes your modified tracked files and stages changes, storing them away without committing them to your current branch. This cleans your working directory, allowing you to switch branches freely.

Here’s how it works:

  1. Navigate to your project’s directory in the terminal.
  2. Run git stash push -u -m “Your descriptive message”. The -u flag includes untracked files, and -m allows you to add a descriptive message to your stash.
  3. Switch to your desired branch using git switch <branch_name> or git checkout <branch_name>.
  4. Once you’re back on the original branch, retrieve your changes with git stash pop.

Leveraging git commit for a More Permanent Solution

Committing your changes is a straightforward way to preserve your work before switching branches. Although this adds a commit to your history, it ensures that your work is safely stored. This method is particularly useful if your changes represent a logical unit of work, even if it’s not yet ready for a final commit.

You can then switch branches and later merge or cherry-pick this commit to integrate your changes into the desired branch. This provides a clear and organized way to manage your work across branches.

Consider using a descriptive commit message like “WIP: Temporary commit before switching branches” to indicate the purpose of the commit.

Employing git worktree for Parallel Branch Development

The git worktree command allows you to check out multiple branches simultaneously in separate directories. This is especially useful for working on different features or bug fixes in parallel. Each worktree operates independently, allowing you to modify and commit changes without affecting other branches.

To create a new worktree:

  • Run git worktree add ../<new_directory_name> <branch_name>.
  • This creates a new directory linked to the specified branch.

This powerful feature streamlines parallel development, eliminating the need for constant branch switching and stashing.

The git checkout -m Option for Partial Changes

Sometimes, you may want to switch branches while keeping only certain modified files. The git checkout -m <branch_name> command attempts to merge the changes from the target branch into your current branch, allowing you to keep your local modifications. This approach is helpful when you want to selectively apply your changes to the new branch.

However, be cautious when using this command, as it can lead to merge conflicts if the same files have been modified on both branches. It’s crucial to review the changes carefully and resolve any conflicts before committing.

Learn more about advanced Git techniques.

Choosing the Right Strategy

Selecting the optimal approach depends on your specific needs and workflow. For small, temporary changes, git stash is ideal. For more substantial modifications or logical units of work, git commit offers a more structured approach. If you need to work on multiple branches concurrently, git worktree is the most powerful solution. git checkout -m offers granular control but requires careful attention to potential merge conflicts.

  • Quick temporary storage: git stash
  • Organized commits: git commit
  • Parallel development: git worktree
  • Selective merging: git checkout -m

Place infographic here illustrating the different strategies and their use cases.

Frequently Asked Questions

Q: What if I have untracked files?

A: Use the -u flag with git stash push to include untracked files in the stash. Alternatively, you can add and commit the untracked files before switching branches.

By understanding and utilizing these techniques, you can significantly improve your Git workflow, managing branches and local changes efficiently and confidently.

Mastering these Git branch switching techniques will significantly enhance your development workflow. Experiment with these different approaches to discover the best fit for your projects. Explore further resources like the official Git documentation (external link) and online tutorials (external link) to deepen your understanding. Remember, efficient branch management is key to a streamlined and productive development process. Check out this resource on advanced branching strategies (external link) for more in-depth knowledge.

Question & Answer :
Alright, lets say one day we make happen to make a bunch of modifications and when we go to commit them we notice we were working on the wrong branch.

How can we force git to switch branches without discarding local changes.

I’m probably going to go about this in a naive way while I wait a reply, but I would like to know if theres a correct procedure as I’d be lying if I said this hasn’t happened to me before…

  • Backup changed repo
  • git reset --hard
  • git checkout right-branch
  • Restore changes
  • git commit -m "changes"

There are a bunch of different ways depending on how far along you are and which branch(es) you want them on.

Let’s take a classic mistake:

$ git checkout master ... pause for coffee, etc ... ... return, edit a bunch of stuff, then: oops, wanted to be on develop 

So now you want these changes, which you have not yet committed to master, to be on develop.

  1. If you don’t have a develop yet, the method is trivial:

    $ git checkout -b develop 
    

    This creates a new develop branch starting from wherever you are now. Now you can commit and the new stuff is all on develop.

  2. You do have a develop. See if Git will let you switch without doing anything:

    $ git checkout develop 
    

    This will either succeed, or complain. If it succeeds, great! Just commit. If not (error: Your local changes to the following files would be overwritten ...), you still have lots of options.

    The easiest is probably git stash (as all the other answer-ers that beat me to clicking post said). Run git stash save or git stash push,1 or just plain git stash which is short for save / push:

    $ git stash 
    

    This commits your code (yes, it really does make some commits) using a weird non-branch-y method. The commits it makes are not “on” any branch but are now safely stored in the repository, so you can now switch branches, then “apply” the stash:

    $ git checkout develop Switched to branch 'develop' $ git stash apply 
    

    If all goes well, and you like the results, you should then git stash drop the stash. This deletes the reference to the weird non-branch-y commits. (They’re still in the repository, and can sometimes be retrieved in an emergency, but for most purposes, you should consider them gone at that point.)

The apply step does a merge of the stashed changes, using Git’s powerful underlying merge machinery, the same kind of thing it uses when you do branch merges. This means you can get “merge conflicts” if the branch you were working on by mistake, is sufficiently different from the branch you meant to be working on. So it’s a good idea to inspect the results carefully before you assume that the stash applied cleanly, even if Git itself did not detect any merge conflicts.

Many people use git stash pop, which is short-hand for git stash apply && git stash drop. That’s fine as far as it goes, but it means that if the application results in a mess, and you decide you don’t want to proceed down this path, you can’t get the stash back easily. That’s why I recommend separate apply, inspect results, drop only if/when satisfied. (This does of course introduce another point where you can take another coffee break and forget what you were doing, come back, and do the wrong thing, so it’s not a perfect cure.)


1The save in git stash save is the old verb for creating a new stash. Git version 2.13 introduced the new verb to make things more consistent with pop and to add more options to the creation command. Git version 2.16 formally deprecated the old verb (though it still works in Git 2.23, which is the latest release at the time I am editing this).