Learn how to untrack a committed file in Git and restore it from the repository using --source=HEAD! 🚀
In this hands-on tutorial, you’ll understand what happens when you use git rm --cached, why git restore fails for untracked files, and how to correctly restore files back into the staging area.
This is a must-know concept for mastering Git file states and avoiding common mistakes.
🎯 What You’ll Learn
👉 How to untrack a file using git rm --cached
👉 Difference between tracked and untracked files
👉 Why git restore fails for untracked files
👉 How to restore files from repository using source HEAD
👉 Understanding git status -s output (D and ?? states)
👉 How staging, working directory, and repository interact
🧪 Hands-On Lab Covered
Creating a file in working directory
Staging and committing a file
Validating repository using:
git show --name-only
git log --name-only
git show HEAD:file1.txt
Removing file from staging using git rm --cached
Understanding D and ?? states
Verifying staging using git ls-files
Restoring file into staging from repository
Validating working directory and staging
💡 Critical Concept (Very Important)
👉 After git rm --cached:
File is removed from staging
File becomes untracked
File still exists in working directory
Repository still has the file
👉 That’s why normal restore fails:
git restore file1.txt ❌ (fails)
👉 Because Git cannot find the file in staging
🔥 Correct Way to Restore
Use:
git restore --source=HEAD --staged file1.txt
👉 source HEAD → fetch from repository
👉 staged → restore into staging area
🔑 Key Insight
👉 Untracking does NOT delete the file from disk
👉 git rm --cached removes file from index (staging)
👉 Repository still keeps the file safe
👉 You can always restore from HEAD
⚡ Key Takeaway
👉 If a file is untracked, Git restore will fail by default
👉 You must explicitly specify the source (HEAD) to restore it
🔑 Commands Used
git status -s
git ls-files
git add file1.txt
git commit -m "Adding file1"
git rm --cached file1.txt
git restore --source=HEAD --staged file1.txt
git show --name-only
git log --name-only
🎯 Who This Video Is For
Beginners learning Git
Students preparing for interviews
Developers confused about untracking vs unstaging
👍 If you found this helpful
Like 👍 | Share 🔁 | Subscribe 🔔
Auf dieser Seite können Sie das Online-Video Git Untrack File Explained | git rm --cached & Restore from Repository (HEAD) mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Sachin Walia 20 März 2026 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 55 Mal angesehen und es wurde von 8 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!