Move existing, uncommitted work to a new branch in Git

In this article you will learn how to commit upstaged changes to another branch or how to commit untracked changes to another git branch.

 


You can use the command git stash to temporarily save your uncommitted changes and then use git checkout to switch to a new branch. Then you can use git stash apply to reapply the changes to the new branch.

Here is an example:

git stash # save your uncommitted changes
git checkout -b new-branch # create and switch to a new branch
git stash apply # reapply the saved changes to the new branch

 

 

Alternatively, you can use git stash branch command, which will create and switch to new branch and apply stash in one command.

git stash branch new-branch

 

 

To commit changes in Git, you can use the git commit command. First, you will need to stage the changes you want to commit using git add. You can stage all changes by using git add ., or you can stage specific files by listing them after git add. Once the changes are staged, you can commit them with a commit message using the git commit command.

Example of git commit changes

git add file1.txt file2.txt # stage specific files
git commit -m "Commit message describing the changes" # commit the changes with a message

Alternatively, you can use git commit -a command, which will stage and commit all modified file in one command.

 

git commit -a -m "Commit message describing the changes"

You can also use git gui or gitk for a graphical representation of the git repository and interactively select which files to stage and commit.


Tags:

git

Share:

Related posts