Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Git: How to checkout previous commit without committing changes?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading