Skip to content
Fix Code Error

How to find and restore a deleted file in a Git repository

March 13, 2021 by Code Error
Posted By: Anonymous

Say I’m in a Git repository. I delete a file and commit that change. I continue working and make some more commits. Then, I find I need to restore that file.

I know I can checkout a file using git checkout HEAD^ foo.bar, but I don’t really know when that file was deleted.

  1. What would be the quickest way to find the commit that deleted a given filename?
  2. What would be the easiest way to get that file back into my working copy?

I’m hoping I don’t have to manually browse my logs, checkout the entire project for a given SHA and then manually copy that file into my original project checkout.

Solution

Find the last commit that affected the given path. As the file isn’t in the HEAD commit, this commit must have deleted it.

git rev-list -n 1 HEAD -- <file_path>

Then checkout the version at the commit before, using the caret (^) symbol:

git checkout <deleting_commit>^ -- <file_path>

Or in one command, if $file is the file in question.

git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"

If you are using zsh and have the EXTENDED_GLOB option enabled, the caret symbol won’t work. You can use ~1 instead.

git checkout $(git rev-list -n 1 HEAD -- "$file")~1 -- "$file"
Answered By: Anonymous

Related Articles

  • Checkout another branch when there are uncommitted…
  • Git Using Remote Branch
  • Retrieve the commit log for a specific line in a file?
  • In plain English, what does "git reset" do?
  • How do I make a Git commit in the past?
  • git pull while not in a git directory
  • What's the difference between "git reset" and "git…
  • What is the origin of foo and bar?
  • Retrieve specific commit from a remote Git repository
  • Why do I have to "git push --set-upstream origin "?
  • What's the difference between HEAD^ and HEAD~ in Git?
  • How can I move HEAD back to a previous location?…
  • Git workflow and rebase vs merge questions
  • How can I resolve Web Component Testing error?
  • Git submodule head 'reference is not a tree' error
  • Why do git fetch origin and git fetch : behave differently?
  • How can I switch to another branch in git?
  • How can I reconcile detached HEAD with master/origin?
  • How do I revert a Git repository to a previous commit?
  • What is your most productive shortcut with Vim?
  • How to git reset --hard a subdirectory?
  • How to retrieve a single file from a specific…
  • How to pull in changes from skeleton sub-repository…
  • How to track untracked content?
  • AppCompat v7 r21 returning error in values.xml?
  • Homebrew install specific version of formula?
  • Various ways to remove local Git changes
  • Squash the first two commits in Git?
  • Is it safe to shallow clone with --depth 1, create…
  • Why does git perform fast-forward merges by default?
  • git lfs push to github failure on Ubuntu 18.04
  • Usage of __slots__?
  • Why does Git say my master branch is "already up to…
  • Generate sequence of dates for given frequency as…
  • How to revert multiple git commits?
  • Git: Create a branch from unstaged/uncommitted…
  • Recalculate merge conflicts (ie. how to generate…
  • Why did my Git repo enter a detached HEAD state?
  • How to modify existing, unpushed commit messages?
  • Git submodule update
  • How to cherry pick a range of commits and merge into…
  • What is git tag, How to create tags & How to…
  • Git - Pushing code to two remotes
  • Git merge with force overwrite
  • How to recover stashed uncommitted changes
  • Move the most recent commit(s) to a new branch with Git
  • What is the copy-and-swap idiom?
  • How to fix Git error: object file is empty?
  • How does origin/HEAD get set?
  • How can I merge two commits into one if I already…
  • Make: "nothing to be done for target" when invoking…
  • Does "git fetch --tags" include "git fetch"?
  • Custom handling forward slashes in vue router ids
  • How can I find the location of origin/master in git,…
  • Difference between variable declaration syntaxes in…
  • How to undo local changes to a specific file
  • Change a Git remote HEAD to point to something…
  • Why is Git better than Subversion?
  • Creating an instance of class
  • git status shows modifications, git checkout --…
  • How to find the nearest parent of a Git branch?
  • Practical uses of git reset --soft?
  • git push says "everything up-to-date" even though I…
  • Logging best practices
  • Why does the function named "traverse" not work on my code?
  • How to get just one file from another branch
  • Git, How to reset origin/master to a commit?
  • Why call git branch --unset-upstream to fixup?
  • "git rm --cached x" vs "git reset head --​ x"?
  • Merge, update, and pull Git branches without using checkouts
  • How to unstash only certain files?
  • Simple tool to 'accept theirs' or 'accept mine' on a…
  • git switch branch without discarding local changes
  • Git merge master into feature branch
  • Class vs. static method in JavaScript
  • Checking out Git tag leads to "detached HEAD state"
  • “tag already exists in the remote" error after…
  • How do I 'overwrite', rather than 'merge', a branch…
  • How can I manually compile a svelte component down…
  • What's the difference between HEAD, working tree and…
  • Setting up and using Meld as your git difftool and mergetool
  • How to configure Git post commit hook
  • What is "git remote add ..." and "git push origin master"?
  • How do I use 'git reset --hard HEAD' to revert to a…
  • How do I include certain conditions in SQL Count
  • Difference between git checkout --track…
  • How to use Git Revert
  • How do I delete a Git branch locally and remotely?
  • pandas: best way to select all columns whose names…
  • How to resolve "local edit, incoming delete upon…
  • How does PHP 'foreach' actually work?
  • Is this request generated by EF Core buggy or is it my code?
  • How to list branches that contain a given commit?
  • Why does git say "Pull is not possible because you…
  • List changes unexpectedly after assignment. How do I…
  • How would I be able to multiple select and pass data…
  • Remove sensitive files and their commits from Git history
  • Windows git "warning: LF will be replaced by CRLF",…
  • Why cat does not work with parameter -0 in xargs?
  • How do I undo the most recent local commits in Git?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to delete files/subfolders in a specific directory at the command prompt in Windows

Next Post:

Opacity of background-color, but not the text

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error