JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Move existing uncommitted changes to a new branch in Git

Author:JIYIK Last Updated:2025/03/28 Views:

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

You might be working on your master branch and realize that you have to create a new branch and move your uncommitted changes over. If you are in a similar situation, stay here.


Move existing uncommitted changes to a new branch

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

You have to create a new branch, move your uncommitted changes and reset your master branch. What do you think of this?

There are two ways to do this.

  1. Use git checkoutthe command or git switchthe command.
  2. Use git stashthe command.

git checkout and git switch commands

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

Assuming we are on the master branch, our workspace 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 our changes to a new branch, for example, a new develop branch, we would run:

$ git checkout -b development

Alternatively, we can use git switchthe command as shown below.

$ git switch -c development

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

Development branch workspace

We can then add the file to the index and commit it.

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 git stashmove our changes to a new branch using the command. This will store the uncommitted changes in a patch file.

$ git stash

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

$ git checkout -b <new branch>

Pop the stash to apply uncommitted changes in the workspace of the newly created branch. Use the git stash popor git stash applycommand.

$ git stash pop

You may encounter merge conflicts, which you will have to resolve manually to your liking. If you are happy with the changes, go ahead and commit.

In short, to move uncommitted changes from one branch to another, you have two options. You can use git stashthe sh command or the regular swipe git switchand git checkoutswipe commands.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Git authentication

Publish Date:2025/03/28 Views:163 Category:Git

This article demonstrates connecting a local repository to a remote repository on GitHub/Gitlab without getting 身份验证失败 error messages. Creating a local repository from scratch in Git To create a local repository from scratch, fo

Log graphs in Git

Publish Date:2025/03/28 Views:96 Category:Git

This article shows you how to use git log the command to graphically view the commit history in Git. Viewing log graphs in Git The command git log displays all the repository history at once snapshots(commits) . This command has a default f

Git refresh remote branch

Publish Date:2025/03/28 Views:93 Category:Git

Git is considered to be the most accurate and the most used software by developers in their projects and can be operated by multiple developers simultaneously. It provides many unique and quirky features to the developers which are very dif

Updating Git on Mac

Publish Date:2025/03/28 Views:181 Category:Git

When working on Git, you should stay updated with the latest version to get its latest features. This article will discuss how to install and update the latest version of Homebrew and Git on your personal computer. Homebrew on Mac Homebrew

Enable Git Tab Auto-Complete

Publish Date:2025/03/28 Views:109 Category:Git

This tutorial demonstrates how to enable git tab autocompletion. Importance of enabling Git Tab auto-completion When developers work with source code, they mostly prefer Git as it is a very familiar and convenient platform for developers th

Restoring a repository in Git

Publish Date:2025/03/28 Views:158 Category:Git

Sometimes while using Git, we come across a situation where we want to pull the latest changes from the remote repository and it conflicts with the existing modifications or files, then we have to push those files to the storage. Git provid

Undo Git Stash Pop conflicts

Publish Date:2025/03/28 Views:178 Category:Git

You can undo this using the solutions in this article git stash pop with merge conflicts . We show you how to abort an erroneous stash pop operation and return to a clean state. But we also demonstrated a git stash pop way to resolve the co

Resolving Git stash conflicts without committing

Publish Date:2025/03/28 Views:72 Category:Git

This article outlines the steps you should follow to resolve Git stash conflicts without reverting or creating commits. For a simpler context, we will simulate a situation where the git stash pop command results in a conflict and try to res

Git Stash needs to be merged

Publish Date:2025/03/28 Views:128 Category:Git

Git is a stylish platform that provides us with many features, one of the main ones is storage. With this unique feature, we can accumulate a lot of unchanged work that we don't want to commit to our repository when the code is checked in;

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial