I was working on a git project. At some time I want to peek into a previous version of a file. But I do not want to commit the current version as that would leave a trace to an unfinished, usually messy, code. What is the right workflow to achieve this?
-
The UI git-extentions lets your right click a file to check its history. So there got to be a single command for that – TheDudeWithHat Jun 10 '20 at 14:55
-
https://stackoverflow.com/search?q=%5Bgit%5D+view+file+old+commit – phd Jun 10 '20 at 18:11
-
Also if the "peeking previous versions" is for tracing a bug and finding the bad commit, you can use `git bisect`. – aderchox Jun 02 '22 at 18:19
1 Answers
Using git stash
The easiest way is to use git stash
. This will store all your local changes without actually committing anything. Then you can checkout any other branches or any commits from history without worrying about your local changes. When you're ready to get back to your changes, checkout the branch that you were originally working on and use git stash pop
. This will reapply the local changes that were saved. If you then look at your commit history, there's no evidence that anything was ever stashed.
Commit them anyway
Another thing you can do is commit your changes, but don't push them to your remote if you have one (github, bitbucket etc). Then checkout whatever other branch/commit you need to checkout. Once you're ready to get back to your local changes, checkout your original branch and undo the previous commit with git reset HEAD~
. This will undo your temporary changes and leave them unadded. Now you can continue your work and there's no trace there was commit ever made.

- 368
- 3
- 12