A Git repository is a project folder together with the complete history of its changes, which Git stores in a hidden .git directory inside that folder. Run git init in any folder to create one, or git clone <url> to copy an existing one.
The Git documentation defines a repository more formally as a collection of refs (names such as branches and tags) together with an object database containing all objects reachable from those refs. In practice that means two things live side by side: the files you edit, and a database of every saved version of them.
What a Git repository contains
A repository has two parts that people often mix up:
- The working tree. The ordinary files and folders you open and edit. Git's glossary describes it as the tree of actual checked out files, normally the contents of the latest commit plus any local changes you have not committed yet.
- The
.gitdirectory. The database. It holds every commit, every branch name, the repository's settings and the staging area. Git reads and writes it for you, so you rarely open it by hand.
A commit is one point in the project's history: a saved snapshot of the project. The whole history is a set of connected commits. A branch is a line of development, and one repository can track any number of them, although the working tree shows only the checked out branch at a time.
What is inside the .git folder
You can look at it with ls -a (macOS, Linux, Git Bash) or dir /a in Command Prompt. The folder starts with a dot, so most file managers hide it. These entries are documented in the gitrepository-layout page:
| Entry | What it holds |
|---|---|
HEAD | Points to the branch you are currently on. A valid repository must have this file. |
config | Settings that apply to this repository only. |
objects | The object store: the saved file contents and commits. Loose objects are stored in subfolders named by the first two characters of the object name, and packed objects live in objects/pack. |
refs | Branch tips (refs/heads), tags (refs/tags) and remote-tracking branches (refs/remotes). |
index | The staging area: what will go into the next commit. |
hooks | Scripts that Git commands can run. git init installs samples, all disabled until you remove the .sample suffix. |
Example output of listing a new repository and reading HEAD, as shown in the Git documentation (the exact list varies by Git version):
$ ls .git
HEAD config description hooks index info objects refs
$ cat .git/HEAD
ref: refs/heads/main
Never edit files in .git manually unless you know exactly what you are doing. Use Git commands instead, because a hand edit can corrupt the history.
How to create a repository
There are two ways, both from the Pro Git book. You either turn a folder that is not under version control into a repository, or you clone an existing one.
Option 1: git init (start from a folder)
cd my-project
git init
According to the Pro Git book, this creates a new subdirectory named .git containing the repository skeleton. At that point nothing in your project is tracked yet. To start tracking files and save the first snapshot:
git add .
git commit -m "First commit"
If this is the first commit on the machine, Git may ask who you are. See how to set your Git user name and email.
Two details from the git-init manual are worth knowing:
- The initial branch is named after the
init.defaultBranchsetting. Without it, Git falls back tomaster, and the manual says this will change tomainin Git 3.0. To choose the name for one repository, rungit init -b main. - Running
git initinside an existing repository is safe. The manual states it will not overwrite things that are already there.
Option 2: git clone (copy an existing repository)
git clone https://github.com/libgit2/libgit2
This example is from the Pro Git book. It creates a directory named libgit2, initialises a .git directory inside it, pulls down all the data for that repository and checks out a working copy of the latest version. To use a different folder name, add it at the end: git clone https://github.com/libgit2/libgit2 mylibgit. More variants are in git clone command examples.
A clone is a full copy of nearly all the data the server has, not just the latest files. That is why you can read the whole history, switch branches and commit while offline.
How to tell whether a folder is a repository
Run this inside the folder:
git status
In a repository you get a report of the current branch and any changes. Outside one, Git prints an error like the following (example output):
fatal: not a git repository (or any of the parent directories): .git
The "parent directories" part matters. Git looks for .git in the current folder and then in each folder above it. A project subfolder therefore counts as inside the repository that sits further up the tree.
To see the saved history, run git log --oneline. Example output:
a1b2c3d Add login form
9f8e7d6 First commit
Local and remote repositories
The copy on your computer is the local repository. A remote repository, in the glossary's words, is a repository used to track the same project that resides somewhere else. You talk to it with git fetch and git push. The difference between those is covered in git fetch vs pull.
Services such as GitHub host remote repositories. They are a convenient shared location, not a requirement. The local repository is complete on its own. For the hosting side, see what GitHub is used for.
What is a bare repository?
A bare repository has no working tree. The glossary describes it as a directory, normally with a .git suffix such as project.git, where the administrative files that would usually sit inside .git are placed directly in that directory and no files are checked out. Publishers of public repositories usually make bare ones available, because the purpose is to push into and fetch from it, not to edit files there. Create one with git init --bare.
Repository, project and folder: what is the difference?
- Folder: any directory. It becomes a repository only when it has a
.gitdirectory. - Repository: the folder plus its stored history, branches and settings.
- Repo: the common short name for a repository, used in documentation and by GitHub.
One consequence: the history lives inside the folder. Moving or copying the whole folder, including the hidden .git, moves the history with it. Copying only the visible files gives you the current content with no history.
Files you do not want in the repository
A repository tracks what you add to it, and everything committed stays in the history. Keep build output, dependencies and secrets out by listing them in a .gitignore file before the first commit. If an ignore rule seems to be ignored, see why .gitignore is not working.
If git init or git clone fails
If git is not recognised, Git is not installed or not on your PATH. Install it and open a new terminal. If a command reports not a git repository, change into the project folder or run git init there. If you ran git init in the wrong folder, check what it created with ls -a. Deleting the .git folder it made undoes the init, but only do that in a folder where you have not yet made commits you want to keep.
Frequently asked questions
- Is a Git repository the same as a folder?
- No. A repository is a folder that contains a hidden .git directory. The .git directory holds the project history and settings. A normal folder without it is just a folder, and Git commands run inside it fail until you run git init.
- What happens if I delete the .git folder?
- The folder stops being a repository. Your current files stay on disk, but the commit history, branches and tags stored in .git are gone unless another copy exists, such as a remote or a clone on another machine.
- What is the difference between a repository and GitHub?
- A repository is the data Git stores for a project, and it works fully offline. GitHub is a hosting service that keeps a remote copy of a repository so people can share it and collaborate. You can use Git without GitHub.
- Can I have a repository inside another repository?
- Technically yes, since each folder with its own .git directory is its own repository. The outer repository will not track the inner one's files as normal content, so this usually causes confusion. Git offers submodules for the cases where you really need nesting.