How to Apply Git Stash to a Different Branch?

Learn how to "git stash apply" to a different branch

Save Stashed Changes and Pop Them Into a Different Branch

You can quite simply do git stash save on the branch where you have the changes, and then do git stash pop on the branch you want the changes to be in. For example:

git stash save
git checkout destination-branch
git stash pop

Apply Stashed Changes to a New Branch

If you wish to apply stashed changes to a new branch, you can simply create a branch from a stash like so:

git stash
git stash branch new-branch

As you can see, you first need to stash changes before you can apply them to a new branch when using git stash branch.

Apply Stashed Changes to an Existing Branch

If the branch you wish to apply stashed changes to already exists, you could use a temporary branch to help add the stashed changes to it like so:

git stash
git stash branch temp-branch

git add .
git commit

git checkout destination-branch
git merge temp-branch

git branch -D temp-branch

To summarize, here's what we do:

  1. Stash changes;
  2. Create a new temporary branch and apply stashed changes to it;
  3. Add/stage all modified files;
  4. Save a new commit object in the local git repository;
  5. Navigate/checkout to the "desitination branch" where we need the changes;
  6. Merge changes from the temporary branch into the destination branch;
  7. Force-delete the temporary branch from local git repository.

Hope you found this post useful. It was published . Please show your love and support by sharing this post.