Sub topic 2
What a Repository Is and What Lives in .git
This chapter assumes you are comfortable in a terminal (cd, mkdir, ls) and have Git installed. You do not need to have run any Git commands yet. Here you build a working model of what a repository is, so later commands make sense.
A repository is a project plus its history
A Git repository is a project directory together with a database of every recorded version of that project. The files you edit are one part. The history Git keeps about them is the other part.
Git stores that history in a hidden directory named .git, at the top of your project. Everything that makes the directory a repository lives there. If you delete .git, the files stay, but the history is gone and the directory is an ordinary folder again.
Looking inside .git
Create a repository and list the hidden directory. The next chapter explains git init in detail. For now, run it and look at the result.
mkdir peek-inside
cd peek-inside
git init
ls .gitThe exact listing varies by Git version, but you will usually see these entries:
| Entry | What it holds |
|---|---|
| `HEAD` | A pointer to the branch you are currently on |
| `config` | Settings that apply only to this repository |
| `objects/` | The stored content: file data, directory listings, and commits |
| `refs/` | Names such as branches and tags, each pointing at a commit |
| `hooks/` | Sample scripts that Git can run at certain moments |
After you make commits, an index file also appears. It records what you have staged for the next commit.
You can read HEAD because it is a small text file:
cat .git/HEADA new repository prints something like ref: refs/heads/main. It says the current branch is main, and that branch does not yet point at any commit.
Why the history is local
The whole database sits inside your project directory. You can commit, view history, and compare versions without a network connection. Copying the project directory, including .git, copies the entire history with it.
The reverse also holds. Because .git holds the only copy of your history, damage to it can lose work. Do not hand-edit files in .git while you are learning. Use Git commands to change it.
Common errors
- Running Git commands outside a repository. Git prints
fatal: not a git repository. Change into the project directory, or rungit initif it is a new project. - Creating a repository inside another repository. Running
git initin a subfolder of an existing repository creates a second, nested.git. The outer repository then treats the folder in an awkward way. Rungit statusfirst if you are unsure whether you are already inside one. - Looking for
.gitand not seeing it. The name starts with a dot, solshides it. Usels -ato list hidden entries.
Exercise
You run ls in a project directory and do not see .git, but git status works without errors. What is the likely explanation?
Show answer
.git is a hidden directory, so ls does not list it. ls -a shows it. The directory could also be a subfolder of a repository, in which case Git finds the .git in a parent directory.
Exercise
What are two things you lose if you delete the .git directory but keep your project files?
Show answer
You lose the stored history (all past commits) and the repository's branches and settings. The files stay on disk, but Git no longer tracks them.