Git

How to Copy Unstaged Changes From One Branch to Another?

On Git, developers create many branches and each developer works on a separate branch. Sometimes, they start coding on the current branch but later they realize that they are working on the wrong branch which causes them some problems. As a consequence, they are required to copy the unstaged changes from one branch to another.

This post will describe the method of copying the unstaged changes from one branch to another branch.

How to Copy Unstaged Changes From One Branch to Another?

To copy unstaged changes from one branch to another branch, execute the “git switch <branch-name>” or “git checkout <branch-name>” commands. These commands leave the current working branch and switch to another branch while bringing all unstaged changes to the target branch. Check out the below-provided steps to do this:

    • Redirect to the local directory
    • Create a new file
    • View current status
    • Switch to another branch
    • Check the status of the target branch
    • Stage and commit changes
    • Ensure new changes
    • Move back to the previous branch and view the current status

Step 1: Switch to the Required Repository

First, execute the “cd <“repository-path”>” command and redirect to the particular directory:

$ cd "C:\Git\Repo1"

 
Step 2: Generate a New File

Next, create a new file in the current repository with the help of the below-stated command:

$ touch test2.txt

 

Step 3: Check Git Status

Then, view the state of the working directory by checking the Git status:

$ git status

 
It can be observed in the below output that the newly created “test2” file is unstaged:


Step 4: Check Git Branches

Now, type out the “git branch” command to view the list of available branches in the working repository:

$ git branch

 
Here, as you can see, the repository contains three branches and the “master” branch is a current working branch:


Next, choose the target branch and navigate to it. For instance, in our case, the “alpha” branch is the target branch.

Step 5: Move to Target Branch

To leave the current branch and redirect to the target branch, utilize the “git switch” command along with the target branch name:

$ git switch alpha

 
The below-screenshot indicates below that the branch has been switched successfully and now the “alpha” branch is the current working branch:


Step 6: View the Target Branch Status

After that, execute the “git status” command to check the status of the target branch:

$ git status .

 
Here, as you can see, the “test2” file needs to be tracked and committed:


Step 7: Stage and Commit the Changes

Next, add the file to the Git staging area and commit new changes in the target branch:

$ git add . && git commit -m "commit master unstaged file into alpha"

 

Step 8: Verify Committed Changes

Check the Git status again to verify the committed changes:

$ git status .

 
According to the below output, the Git status is clear now and the unstaged changes have been added to the “alpha” branch:


Step 9: Switch Back to Old Branch

Now, after completing the task in the current working branch, move back to the “master” branch using provided command:

$ git checkout master

 

Step 10: Verify Old Branch Status

Lastly, check the status of the “master” branch to verify changes:

$ git status

 
The below output indicates that there is nothing to commit in the old branch:


We have efficiently described the procedure of copying the unstaged changes from one branch to another branch in Git.

Conclusion

To copy the unstaged changes from one branch to another, utilize the “git switch <branch-name>” or “git checkout <branch-name>” commands. These commands will copy the unstaged changes from one branch to the target branch by switching. Then, stage and commit new changes in the target branch. This post explained the method of copying the unstaged changes from one branch to another.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.