Does the .git folder actually contain your files?

In theory, if you backed up just the .git folder in a flash drive and some rouge user deleted all your project files in your PC, can you still restore your files from that .git folder in your flash drive?

Note: This is just for a local repo scenario, I understand you can pull from an online repo.

3 Answers

Yes, most of it. The .git folder contains the complete repository history (all commits and their contents), as well as the staging area (things you've git add-ed but not yet committed).

Indeed, when Git servers store repositories, they use just the .git folder (a "bare" repository).

It doesn't, however, contain copies of miscellaneous files that you haven't yet told Git about. If you made some edits but did not git add them, they won't have been copied into .git/objects/ yet.

The way to extract the files is git checkout -f or git checkout <paths> (which will copy staging area to working tree).

4

The .git directory in a project will have enough information to restore any version in your commit history.


Just for fun, I ran this locally:

cd <my local repo>
rm -r *
ls # nothing there
ls -la # some dot files/directories
git checkout .
ls # my stuff is back!

All my stuff was back (except some uncommitted changes, compile output, etc) and then I immediately regretted this "fun" exercise


That said, the .git directory does contain enough information to restore your files and even your historical commits.

See:

2

What you're talking about is what's known as a 'bare' repository.

In essence, the .git directory contains all the data on all the revisions tracked in that repository. This includes all the data required to reconstruct the working directory for a checkout.

Because of this, if you will never access the individual files of a repository, but only ever clone the repository or push or pull from it, you can save a potentially large amount of space by just having the .git directory.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like