Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I restore one file from an earlier Git commit?

Quick answer

To restore one file from an earlier commit, first run git status and inspect any changes to that file. Preserve edits you may need, for example by copying the file outside the repository. Then run git restore --source <commit> -- <path>. This updates the file in your working tree; it does not move your branch. Check the result and status before deciding what to do next. A file absent from the chosen commit may be removed.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I only want an older version of one file. How do I get it without rolling the whole project back?

Geez

Use git restore with the earlier commit and the file path:

git restore --source <commit> -- <path>

Replace <commit> with the commit you want to take the file from and <path> with its path in the repository. For example, if the placeholders were abc123 and notes.md, the command would be git restore --source abc123 -- notes.md.

This restores that path’s contents in your working tree; it does not move the branch. That is different from trying to move the repository’s branch back to an earlier point. Pause before running it, though: restoring a path can replace its current contents, and if the path is absent from the chosen commit, it may be removed. [S1]

Follow-up question

There are edits in the file now. What should I check before restoring it?

Geez

Start with git status. It reports changes between the current commit (HEAD) and the index, changes between the index and working tree, and untracked paths. git status -v can show staged textual changes; repeating the option also shows unstaged textual changes. [S2]

Read the status output and decide whether the current version contains anything you need. Status helps you identify work to preserve, but it does not back that work up. If you are unsure, stop before restoring and make a separate copy of the file outside the repository, using a clearly labeled name such as [backup-name]. Then you have a reference if the current edits matter.

Follow-up question

Could you walk through a cautious example, including what to do if the file has staged changes?

Geez

Assume you want draft.md from commit abc123; those names are examples, not values to copy blindly.

  1. Run git status -v and review the output for draft.md. Look for staged and unstaged edits, as well as untracked work you may need to keep. [S2]
  2. If the present file has edits you want, preserve them first. A separate copy outside the repository is a simple manual option. Do not treat the status output itself as a backup.
  3. Once you have identified the right commit and path, run git restore --source abc123 -- draft.md. This targets the working-tree file and does not move the branch. [S1]
  4. Inspect the restored file and run git status again. Confirm the working-tree contents are what you intended before taking any further action.

Be especially deliberate if the file is staged: Git distinguishes the index from the working tree, and the default restore location is the working tree. The --staged option affects the index instead. Do not add --staged unless you specifically intend to restore the index as well. [S1][S2]

Follow-up question

How can I tell afterward that I restored the right thing, and what if I picked a commit where the file did not exist?

Geez

Afterward, open the file and compare its contents with what you meant to retrieve; then run git status to review the repository state. If the result is surprising, stop and consult the separate copy you made rather than assuming the prior edits are still present. Status can show repository changes, but it does not establish that the chosen commit was the one you intended. [S2]

A path absent from the selected commit may be removed by the restore operation. [S1] If you are uncertain whether it exists there, verify your intended commit and path before running the command. These steps restore content in the working tree; they do not determine whether or when you should commit the result.

A practical checklist

  • Identify the intended earlier commit and exact file path.

  • Run git status and review staged, unstaged, and untracked work.

  • Preserve current edits you may need; status is not a backup.

  • Run git restore --source <commit> -- <path> only after checking the target.

  • Inspect the restored file and run git status again.

A few more useful answers

Does restoring one file move my branch?

No. This command restores the selected path in the working tree; it does not move the branch. [S1]

Does `git status` save my current edits?

No. It reports repository state and can help you spot changes to preserve, but it does not back them up. [S2]

What does `--staged` change?

The default restore location is the working tree; --staged affects the index. Use it only when you intend to restore the index. [S1]

Sources

References used for this guide.

  1. [S1] Git: git-restore documentation
    git-scm.com · Checked
  2. [S2] Git: git-status documentation
    git-scm.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work