Development Tools Introduction to Git Managing Committed Files Commit SHAs and Undoing Commits

Can you specify which file version to go back to if you committed the file numerous times in a local machine?

For example, If I commit a file and get its first version. I make changes to it later on and commit the new changes to get a second version. I do it again with another change and commit it a third time. At this point, would I be able to go back to the first version? Or am I stuck at only have two versions (2nd and 3rd version) at a time and I'm only able to go back to the second version.

I've gone through this section in the git course, but I'm not sure if I missed a lesson where this is a possibility

1 Answer

You can go back to any version. Say your last three commits change the same file:

f116d82 third version
18bdc5d second version
a65d770 first version

If you want to go back and have the first version, you can go back to that version using the specific SHA:

git reset --hard a65d770

Haven't found this specific case to happen often in real life, though. It's more common for me to revert the changes of a commit to go back to a previous version. Hope this helps

I was just curious but thanks for letting me know! I’m assuming we don’t learn it in this Git module?

git revert would be the command to use, which was discussed in the video you posted this question from. It's just when you want to go back to a first version of a file when it's been edited multiple times throughout a long git history, it can get complicated.

For example, first you'd need to find all the commits where that specific file was edited (not shown in this module). And then if a commit you want to revert touches more than the specific file you want to change, you might have to make more changes after reverting (this is why it's good to keep commits small and specific)