How to Move Existing Uncommitted Changes to a New Branch in Git

John Wachira Feb 15, 2024
How to Move Existing Uncommitted Changes to a New Branch in Git

This article discusses the process of moving uncommitted changes to a new branch.

You may be working on your master branch and realize that you must create a new branch and move the uncommitted changes. If you are in a similar situation, stick around.

Move Existing Uncommitted Changes to a New Branch

Take this hypothetical scenario: you are working on your master branch and realize that your code needs testing before you can commit to the master.

You must create a new branch, move the uncommitted changes and reset your master branch. How do you go about this?

There are two ways of doing this.

  1. Using the git checkout command or the git switch command.
  2. Using the git stash command.

the git checkout and git switch Commands

If you have been using Git for ages, you probably know that the git checkout command has several uses. You know that we can use the git checkout command to create and switch to a new branch.

Assuming we are on our master branch, and our working space looks like this:

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to ...)
  (use "git checkout -- <file>..." to ...)
        modified:   LICENSE.md
        modified:   README.md
Untracked files:
  (use "git add <file>..." to include...)
        scripts.txt
no changes added to commit (use..)

How do we move these changes to a new branch?

To move the changes to a new branch, for example, a new development branch, we will run:

$ git checkout -b development

Alternatively, we can use the git switch command, as illustrated below.

$ git switch -c development

Both commands will create a development branch and move our uncommitted changes to the new branch. Below is our working directory on the development branch.

development branch working space

We can then add the files to the index and commit.

If we switch back to the master branch and run the git status command, we will see that our working tree is clean.

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

the git stash Command

We can move changes to a new branch using the git stash command. This will store the uncommitted changes in a patch file.

$ git stash

You can then create a new branch where you will store the uncommitted changes. Use the git checkout command to create and switch to the new branch, as shown below.

$ git checkout -b <new branch>

Pop the stash to apply the uncommitted changes in the working space of the newly created branch. Use the git stash pop or git stash apply commands.

$ git stash pop

You may get merge conflicts which you will have to resolve manually to your liking. If you are satisfied with your changes, proceed to commit.

In a nutshell, to move uncommitted changes from one branch to another, you have two options. You can use the git stash command or the conventional git switch and git checkout commands.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Stash