How to Merge Local Uncommitted Changes into Another Git Branch

  1. Steps to integrating changes into another branch
    1. Stashing changes of current branch
    2. Checkout to the right branch
    3. Apply changes to the right branch
  2. Another Helpful Use Case of Git Stash
    1. Stash the changes
    2. List the stashed changes
    3. Selecting stashes
  3. Selecting stashes
  4. Checking out Branches

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

After stashing the changes on your working copy with the git stash 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:

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 git stash pop command, which will also remove the changes from the stash.

git stash pop

Another Helpful Use Case of Git Stash

Beside 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

To stash currently untracked files, add the option -u to git stash:

git stash -u

List the stashed changes

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

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 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 git stash list. The git stash 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 -u option or --include-untracked help to 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, git checkout tells Git to record all the new commits. Very often, the git checkout and git clone are associated. The git clone command fetches code from a remote repository.


Do you find this helpful?

Related articles