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

Old Versions of a File

Git restore --source.

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

You liked the home page better the way it was three commits ago. Git still has that version. You can bring back just that one file, without touching anything else.

Old Versions of a File
From the video: Old Versions of a File
  • The old version was better
  • Git still has it
  • Bring back just one file

Bring back an old version

First, the history of the home page. We want the version from the welcome text commit, zero b a six zero four nine. Git restore, dash dash source, that hash, and the file name. Cat shows the file now has just two lines, like back then. Status shows it as modified, so we can commit it, if we're happy.

git
git log --oneline index.html
f63a17a (HEAD -> main) Link to about page
0ba6049 Add welcome text
f7d9757 Add home page
git restore --source 0ba6049 index.html
cat index.html
<h1>My Site</h1>
<p>Welcome!</p>
git status --short
 M index.html

How it works

Dash dash source tells restore where to copy the file from. Any commit hash works. The file in your working folder is replaced with that old version. Nothing in your history changes. It's like copying the file out of an old snapshot.

keyvalue
--source <hash>where to copy from
<file>which file
Historyunchanged
Thencommit, if you like it

Summary

git restore dash dash source copies a file out of any old commit. Next, visiting a whole old snapshot.

Key takeaways
  • git restore --source <hash> <file>
  • Copies an old version
  • Commit it if you like it
Next up: Visiting an Old Commit — time travel