AI Hints
Home›Git & GitHub Tutorial›Going Back in Time
Git & GitHub · Lesson 28 of 144

Visiting an Old Commit

Time travel.

⏱ 2 min read▶ Video at 29:28🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

What did the whole project look like back then? You can travel back, and look around, in any old commit. All your files change to exactly how they were.

Visiting an Old Commit
From the video: Visiting an Old Commit
  • See the whole project, back then
  • Every file changes
  • Then come back

Going back

Right now we have three files. Let's travel to the second commit, zero b a six zero four nine. We type git switch, dash dash detach, and the hash. Git says head is now at that commit. Now list the files. Only index dot HTML. Back then, the about page and the styles didn't exist yet.

git
ls
about.html
index.html
style.css
git switch --detach 0ba6049
HEAD is now at 0ba6049 Add welcome text
ls
index.html

Coming home

To come back to the present, switch back to main. All three files are back. Nothing was lost. Time travel in Git is completely safe, as long as you only look around.

git
git switch main
Previous HEAD position was 0ba6049 Add welcome text
Switched to branch 'main'
ls
about.html
index.html
style.css

Summary

git switch dash dash detach visits any old commit, and git switch main brings you home. Next, what that scary detached head message means.

Key takeaways
  • git switch --detach <hash>
  • Look around safely
  • git switch main to return
Next up: Detached HEAD — what it means