Many a time, you must have faced issues where you need to save your current changes in the project without committing it.
Suppose you are working on a branch and you want to rollback the changes, but also changes are important so you also want to save those changes somewhere.
For this situation we have another feature in git, that is called git stash.
Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory
You can use the following commands:
- To save your uncommitted changes
git stash
# to add a description about the stash
git stash push -m "message"
- To list your saved stashes
stash@{0}: On develop: remove auth router
stash@{1}: On develop: upgrade moment to v10
- To apply/get back the uncommited changes where x is 0,1,2...
git stash apply stash@{1}
git stash apply stash@{x}
Note:
- To apply a stash and remove it from the stash list
git stash pop stash@{x}
- To apply a stash and keep it in the stash list
git stash apply stash@{x}
If you want to create another branch with the changes in stash you can do,
git stash branch <branchname> [<stash>]
If git stash
seems complicated, there is another way to save the current changes and apply them later.
# save your working copy changes
git diff > some.patch
# re-apply it later
git apply some.patch