Welcome to our comprehensive guide on git stash apply! This tutorial is designed to help you understand how to save and apply changes when you need to switch branches but don't want to commit yet. Let's dive in! šÆ
git stash is a Git command that helps you save your uncommitted changes. It's like a temporary storage for your work, allowing you to switch branches, work on something else, and later come back to your original work. š”
Sometimes, you might be in the middle of making changes and need to switch to another branch for some reason. But if you switch branches without saving your changes, they will be lost! That's where git stash comes in handy. It allows you to store your changes temporarily, switch branches, and apply them back later.
Before we dive into git stash apply, let's see how to stash our changes.
$ git stashThis command will save your uncommitted changes and take you back to the last commit. You can see the stashed changes with:
$ git stash listNow, let's move on to the main topic - git stash apply. After stashing your changes, you might want to apply them back to your current branch. Here's how:
$ git stash applyThis command will apply the latest stashed changes to your current branch.
If you want to both apply and delete the stashed changes, use git stash pop:
$ git stash popš Note: If you have stashed changes on a branch that has been advanced, applying the stash might lead to merge conflicts if there are changes in the same files. In such cases, you'll need to resolve the conflicts manually before applying the stash.
What does `git stash` command do?
Let's see a practical example of using git stash and git stash apply.
$ git init
$ touch readme.md
$ git add readme.md
$ git commit -m "Initial commit"$ echo "Updating readme" >> readme.md$ git stash$ git stash list
stash@{0}: WIP on master: Updating readme$ git checkout new_branch$ git checkout master
$ git stash apply$ cat readme.md
Updating readmegit stash drop:$ git stash dropThat's it! You've successfully used git stash apply to save and apply changes in Git. Happy coding! š