W3docs

How to Merge Local Uncommitted Changes into Another Git Branch

Read this tutorial and find solution to the question of merging local uncommitted changes from one Git branch into another. Also, read important tips.

While working on different branches, sometimes, developers can get confused and make changes on a wrong branch. Here, you will figure out how to integrate the changes on the wrong branch to the right one without any conflicts.

Steps to integrating changes into another branch

Let’s discuss each step below:

Stashing changes of current branch

Stash the changes you have made on your current branch with the git stash command:

git stash

git stash

After stashing the changes on your working copy with the <kbd class="highlighted">git stash</kbd> command, you can switch to the right branch and apply them on it.

Checkout to the right branch

Then switch to the other branch with the git checkout command:

switch to the other branch git

git checkout <right-branch>

Apply changes to the right branch

After checking out to the right branch, you can apply the changes to it with the <kbd class="highlighted">git stash pop</kbd> command, which will also remove the changes from the stash.

remove the changes from stash git, git stash pop

git stash pop

Note: If the target branch has diverged from the branch where you made the changes, git stash pop may result in merge conflicts that you will need to resolve.

Another Helpful Use Case of Git Stash

Besides the integration of changes from one branch to another, git stash can be used for storing the changes.

Stash the changes

If you have made some changes that you don't want to commit, but want to save for further usage, then you can stash those changes:

git stash command

git stash

To stash currently untracked files, add the option <kbd class="highlighted">-u</kbd> to <kbd class="highlighted">git stash</kbd>:

git stash currently untracked files

git stash -u

List the stashed changes

To list the stashed changes, you can run the command below:

git check the various stashes

git stash list

Selecting stashes

Then, to apply the changes of the preferred stash version, you need to run the command below, by mentioning the corresponding stash version:

git select the right stash

git stash apply N

Selecting stashes

The git stash command is one of the useful features in Git. It saves your local changes away for further use and then returns them from your working copy. The changes stashed away by git stash can be listed with <kbd class="highlighted">git stash</kbd> list. The <kbd class="highlighted">git stash</kbd> apply takes the top stash and applies it to the Git Repository. The git stash stashes both staged and unstaged changes. However, it will not stash the new, not staged files in the working copy and ignored files. In these cases, the <kbd class="highlighted">-u</kbd> option or <kbd class="highlighted">--include-untracked</kbd> help stash the untracked files.

Checking out Branches

The git checkout command switches branches or restores working tree files operating on files, commits, and branches. Updating the files in the working directory so as to match the version stored in that branch, <kbd class="highlighted">git checkout</kbd> switches your working tree to the target branch without recording new commits. Very often, the <kbd class="highlighted">git checkout</kbd> and git clone are associated. The <kbd class="highlighted">git clone</kbd> command fetches code from a remote repository.