What is the .git folder?

What is the folder called .git?

It's created in a repository. What is contained within it and why is created?


Solution 1:

.git is initialized by git init.

.git contains all information required for version control. If you want to clone your repo, copy .git is enough.

4 sub-directories:

  • hooks/ : example scripts
  • info/ : exclude file for ignored patterns
  • objects/ : all "objects"
  • refs/ : pointers to commit objects

4 files:

  • HEAD : current branch
  • config : configuration options
  • description
  • index : staging area

Here "object" includes:

  • blobs(files)
  • trees(directories)
  • commits(reference to a tree, parent commit, etc)

Solution 2:

The .git folder contains all the information that is necessary for your project in version control and all the information about commits, remote repository address, etc. All of them are present in this folder. It also contains a log that stores your commit history so that you can roll back to history.

For more info, you can check the official website of git.

Solution 3:

This explanation should help beginners to understand the .git folder.

The .git folder is a bit like a magic hat into which you put your current magic show.

When you create a new git repository (git init), everything you organise into a show format is put inside this magic hat and can be 'pulled out' whenever, wherever you want.

After pulling everything out, you can throw everything away when you are finished with the show (i.e. all your files except the .git folder), and you can always pull out exactly the same show at a later date. (As each new show is simply a clone of what is inside the hat).

If you send someone JUST the .git folder, they can always pull out your project files into the same structure (show format) as you put them in.

git add tells the .git folder what is able to be pulled out e.g. a rabbit wearing a tuxedo and holding a cane (or a single file or whole menu bar on your website).

git rm tells the .git folder to stop allowing something to be pulled out of the hat e.g. imagine if you no longer wanted the rabbit to be part of your magic show. (Important to note, that you can still recover a previous version of your show, which would include the rabbit (your 1999 version of your blog with Comic Sans), if you really wanted, but your current show would not include the rabbit if you used git rm).

Solution 4:

Basiclly, it means your dir is handled by Git (Git repository). If you move it somewhere else (or delete it), you'll face something like:

fatal: Not a git repository (or any of the parent directories): .git

every time you use 'git *' command there.

You can move the .git directory somewhere else using:

git --git-dir=/myproject_path/myproject.git log --oneline

Or:

export GIT_DIR=/myproject_path/myproject.git

But i don't recommend doing it. Pay attention that it is only 1 folder, unlike SVN.

It holds all relevant info for GIT to handle your code, like the position of the HEAD, hooks to apply before/after commit/push and some other files.

Maybe most "famous" file inside is the config file which holds all your branches info.

Recommend to read here more info.

Solution 5:

This is the "thing" which makes your project a "git" repository. The .git folder is the directory which is created when you do git init (in case of a new project) or you do git clone (in the case of pulling a project from somewhere else). Without .git, your project is a local project and not a git project, that means you cannot perform any git operations.

git stores the metadata and object database for the project in this directory like:

  1. Remote information (to which remote server your project is connected)
  2. History of all local commits
  3. Branch information (on which branch is your current project state (HEAD) pointing to)
  4. All logs of all local commits you have ever made (including revert changes)

To know more, check the official documentation from git: https://git-scm.com/book/en/v1/Git-Basics-Getting-a-Git-Repository