I want to checkout a previous commit, but I’ve made changes, so it’s prompting me to commit or stash the changes first. I don’t have the changes at the point where I want to commit them yet. Is this what git stash would work for? Would it save my changes such that if I checkout the previous commit and then return to HEAD, finish my changes and then commit, all my changes from before and after stashing would be included in that commit?
>Solution :
Yup, that’s exactly what git stash is for. It will save your changes, and you will be able to restore them later with git stash pop. (That’s the simple usage. git stash pop will get the last thing you saved.)
Say you were working on main.
git stash # Saves and removes your changes
git checkout HEAD^ # Checkout previous commit
# Play around here.
git checkout main # Go back to the branch you were using.
git stash pop # Restore your work.