Move existing uncommitted changes to a new branch in Git
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.
-
Use
git checkout
the command orgit switch
the command. -
Use
git stash
the 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 checkout
the 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 switch
the 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.
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 stash
move 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 checkout
the 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 pop
or git stash apply
command.
$ 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 stash
the sh command or the regular swipe git switch
and git checkout
swipe 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.
Related Articles
Git installation and establishment of local warehouse service
Publish Date:2025/04/05 Views:89 Category:Git
-
Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper
git remote operation——multiple remote repositories for one project
Publish Date:2025/04/05 Views:131 Category:Git
-
Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓
Git cherry pick command usage
Publish Date:2025/04/05 Views:190 Category:Git
-
git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an
Comparison between Git merge and Git rebase
Publish Date:2025/04/05 Views:171 Category:Git
-
The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th
How to fix Git error Error: src refspec master does not match any
Publish Date:2025/04/05 Views:124 Category:Git
-
When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to
Rebase local branch when pulling changes from remote repository branch in Git
Publish Date:2025/04/05 Views:144 Category:Git
-
This article will cover the basics of rebasing your local branch when pulling changes from a remote repository branch in Git. We use the version control system Git to track changes made to files. We commit changes in a local branch in our l
Undo Git Stash
Publish Date:2025/04/04 Views:187 Category:Git
-
This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash
View a list of cache entries in Git
Publish Date:2025/04/04 Views:59 Category:Git
-
We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T
Git stores specific files
Publish Date:2025/04/04 Views:115 Category:Git
-
This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f