Omega
Omega

Reputation: 1599

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 it created?

Upvotes: 89

Views: 164692

Answers (8)

serv-inc
serv-inc

Reputation: 38267

Seems like noone has yet posted the current 2024 official documentation

TL;DR: either a directory with internal data, or a link to the directory somewhere else

NAME

gitrepository-layout - Git Repository Layout

SYNOPSIS

$GIT_DIR/*

DESCRIPTION

A Git repository comes in two different flavours:

  • a .git directory at the root of the working tree;

  • a .git directory that is a bare repository (i.e. without its own working tree), that is typically used for exchanging histories with others by pushing into it and fetching from it.

Note: Also you can have a plain text file .git at the root of your working tree, containing gitdir: to point at the real directory that has the repository. This mechanism is called a gitfile and is usually managed via the git submodule and git worktree commands. It is often used for a working tree of a submodule checkout, to allow you in the containing superproject to git checkout a branch that does not have the submodule. The checkout has to remove the entire submodule working tree, without losing the submodule repository.

These things may exist in a Git repository.

objects

Object store associated with this repository. Usually an object store is self sufficient (i.e. all the objects that are referred to by an object found in it are also found in it), but there are a few ways to violate it.

  1. You could have an incomplete but locally usable repository by creating a shallow clone. See git-clone1.

  2. You could be using the objects/info/alternates or $GIT_ALTERNATE_OBJECT_DIRECTORIES mechanisms to borrow objects from other object stores. A repository with this kind of incomplete object store is not suitable to be published for use with dumb transports but otherwise is OK as long as objects/info/alternates points at the object stores it borrows from.

    This directory is ignored if $GIT_COMMON_DIR is set and "$GIT_COMMON_DIR/objects" will be used instead.

objects/[0-9a-f][0-9a-f]

A newly created object is stored in its own file. The objects are splayed over 256 subdirectories using the first two characters of the sha1 object name to keep the number of directory entries in objects itself to a manageable number. Objects found here are often called unpacked (or loose) objects.

[...] (go read there for further details)

Upvotes: 0

John
John

Reputation: 134

The inner workings of the .git folder can be found here, hiding in the last chapter of the Official Git Documentation:

10.1 Git Internals - Plumbing and Porcelain

Upvotes: 0

user
user

Reputation: 977

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 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 on their homepage: 2.1 Git Basics - Getting a Git Repository

Upvotes: 5

GraceMeng
GraceMeng

Reputation: 1109

Folder .git is initialized by git init.

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

Four sub-directories:

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

Four files:

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

Here "object" includes:

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

Upvotes: 59

timhc22
timhc22

Reputation: 7451

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. (It is 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).

Upvotes: 14

Aaron_ab
Aaron_ab

Reputation: 3758

Basically, it means your directory 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 one folder, unlike SVN.

It holds all relevant information 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 configuration file which holds all your branches information.

I recommend to read here more information.

Upvotes: 3

Avinash Ranjan
Avinash Ranjan

Reputation: 611

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 information, you can check the official website of Git.

Upvotes: 27

Anton Strogonoff
Anton Strogonoff

Reputation: 34142

Technically that .git directory is the Git repository itself. All contents of the repository, including all file versions, tags, branches, etc., are spread across files within this directory.

Things immediately outside .git comprise the working directory, which is an optional accomodation to make working with your Git repository easier. By default working directory reflects the latest commit of your local repository, but you can switch your working directory tree to different commits within the repository, and modify/create/delete files within the working directory before making a new commit.

Without a working tree you can still have a bare Git repository—it consists only of the .git directory (normally named <something>.git in this scenario) and nothing else. It’s not commonly used except on Git servers.

Upvotes: 2

Related Questions