Recovering Deleted Files From Your Git Working Tree There are times when mistakes happen, and useful and important files are deleted by error or lost from your file system irrevocably (or seemingly, at least). Version control systems make it difficult to permanently lose files, provided they have been either added to staging or committed to a remote repository, because Git allows you to undo or revert changes and access previous versions of the saved files. It is also possible to erroneously erase files from both the working directory and the Git repository. I’ve certainly done that! I imagine you have, too, if you’re reading this, and if that’s the case, then you will need a way to recover those files. I have a few methods and strategies you can use to recover your deleted files. Some are more obvious than others, and some are designed for very specific situations. And while it is indeed possible to irrevocably lose a file, even then, you may have a path to at least recover a copy of it with third-party software if it comes to that. How Git Works With Files Before we dive into all of that, let’s explore how your files journey from your local computer to your remote repository. Your files are initially only located on your computer’s storage, known as your working tree or working directory , and Git has no idea they exist yet. At this point, they are at their most vulnerable state since they are untracked. Adding files to the staging area — also known as the index — so that Git is aware of them is what the git add
That will take things back to the last commit that was made. But let’s say you’ve made several commits since the file was deleted. If that’s the case, try checking out a specific commit by providing that commit’s hash: git checkout
Oh, you’re not sure which file it is, or there are more files than you want to type out? You can check out the entire working tree by committing the filename: git checkout
git reset git reset If you have committed the changes that deleted the files and directories, then you can use the git reset command to reset the HEAD pointer to a previous commit. This will also overwrite the working tree with the contents of the specific commit, and any deleted files and directories will be restored in the process. git reset
git restore git restore If you want to restore deleted files and directories without overwriting the working tree, then you can use the git restore command. This command restores files and directories deleted from the staging area or the working tree. Note that it only works for tracked files, meaning that any files that weren’t git add -ed to the working tree are excluded. git restore –staged
To jump back one commit, you could go back to the –worktree instead of the staging area: git restore –worktree
And, of course, leave out the filename if you want to restore all files in the working tree from the previous commit: git restore –worktree
Another option is to restore all of the files in the current directory: git restore .
git reflog git reflog There’s also the git reflog command, which shows a history of all recent HEAD movements. I like this as a way to identify the commit that you want to checkout or reset to. git reflog
Last Resorts When files that are neither present in the staging area nor committed are deleted from the working tree, it is commonly accepted that those files are gone forever — or oti lor as we say in Yoruba — without any hope of recovery. So, if for any reason or by error, you delete important files from your project’s working tree without ensuring that they are either in the staging area or have been previously committed, then you may be thinking all hope of getting them back is lost. But I can assure you, based on my experiences in this situation, that it is usually possible to recover all or most of a project’s lost files. There are two approaches I normally take. File Recovery Apps File recovery tools can recover lost or deleted data from your storage devices. They work by running a deep scan of your device in an attempt to find every file and folder that has ever existed on your storage device, including deleted and lost files and folders. Once the files have all been found, you can then use the data recovery tool to restore/recover the files of your choice to a new location. Note : Some of the deleted and lost files found may be corrupted and damaged or not found at all, but I am certain from my experience using them that the majority will be found without any corruption or damage. There are a variety of file recovery tools available, and the “right” one is largely a subjective matter. I could spend an entire post exclusively on the various options, but I’ve selected a few that I have used and feel comfortable at least suggesting as options to look into. Wondershare Recoverit is capable of recovering more than 1,000 file formats. Its free tier option allows you to run a scan to find files on your computer’s storage, but to actually recover the files, you will have to do a paid upgrade to one of its paid plans starting at a $69.99 annual subscription or a one-time $119.99 license. There’s a premium plan for more enhanced recovery methods for things like videos and files, as well as fixing corrupted files that go well beyond the basic need of recovering a single lost file. Pros: High success rate, free tech support, allows partition recovery. Cons: Free tier is extremely limited. EaseUS Data Recovery Wizard is perhaps one of the most popular tools out of what’s available. Its free tier option is quite robust, running a deep scan and recovering up to 2GB of data. The difference between that and its paid subscription (starting at $119.95 per year, $169.95 lifetime) is that the paid tier recovers an unlimited amount of data. Pros: Fast deep scans, file preview before recovery, easy to use, generous free tier. Cons: Paid plans are significantly more expensive than other tools, Windows and macOS versions are vastly different, and the macOS software is even more expensive. DM Disk Editor (DMDE) makes use of a special algorithm that reconstructs directory structures and recovers files by their file signature when recovering solely by the file system proves impossible. DMDE also offers a free tier option, but it is quite limited as you can only recover files from the directory you have selected, and it only recovers up to 4,000 files at a time. Compare that to its paid versions that allow unlimited and unrestricted data recovery. Paid plans start at $20 per year but scale up to $133 per year for more advanced needs that are likely beyond the scope of what you need. Pros: High recovery success rate, generous free tier, reasonable paid tiers if needed. Cons: I personally find the UI to be more difficult to navigate than other apps. Software Operating Systems supported Starting price File types and formats supported Wondershare Recoverit Windows, Mac, Linux(Premium) $69.99/year 1000+ file types and formats EaseUS Windows, Mac $99.95/year (Windows), $119.95/year (Mac) 1000+ file types and formats DMDE Windows, Mac, Linux, DOS $20/year Supports basic file formats. Does not support raw photo files. As I said, there are many, many more options out there. If you’re reading this and have a favorite app that you use to recover lost files, then please share it in the comments. The more, the merrier! Last Resort: git fsck git fsck First off, the git fsck command can be dangerous if used incorrectly. It is essential to make sure that you understand how to use the command before using it to recover files from the working tree. If you are unsure how to proceed after reading this section, then it is a good idea to consult the Git documentation for additional details on how it is used and when it is best to use it. That said, git fsck can indeed recover files lost from the working tree in Git and maybe your absolute last resort. It works by scanning the Git repository for “dangling” objects, which are objects that are not referenced by any commit. The Git docs define it like this: : dangling object “An unreachable object that is not reachable even from other unreachable objects; a dangling object has no references to it from any reference or object in the repository.” This can happen if a file is deleted from the working tree but not committed or if a branch is deleted, but the files on the branch are not deleted. To recover files lost from the working tree using the git fsck command, follow these steps: Run git fsck –lost-found , which is a special mode of the git fsck command. It creates a directory called .git/lost-found and moves all of the lost objects to that directory. The lost objects are organized into two subdirectories: commits and objects. The /commits subdirectory contains lost commits, and the /objects subdirectory contains lost blobs, trees, and tags. This command prints the dangling objects (blobs, commits, trees, and tags) if they exist. Run the git show
This content was originally published here.