git-init-inicializar-repositorio

Creating Your First Repository with git init

  • 5 min

Once Git is installed and configured, the first step is to transform our working directory into a repository managed by Git.

What is a repository?

It is a workspace that is under Git’s control.

Practically speaking, it’s the normal folder where you have your project files, to which Git adds the information it needs to track changes.

That is, one of Git’s strengths is that it works with normal, ordinary file folders on your computer (no need to set up a central server or anything like that).

Git simply adds the necessary internal structure so the system can do its job (index files and record the change history).

The git init Command

The command to start controlling a folder with Git is git init. This command turns a normal folder into a Git repository.

CommandDescription
git initInitializes a new Git repository in a directory

This command creates the necessary internal structure for Git to function. You can use it both in an empty folder and in one that already has files.

In both cases, the process is the same. Simply:

Open the terminal inside the project folder.

Execute:

git init

Git will respond confirming it has initialized a repository:

Initialized empty Git repository in C:/Proyectos/MiWeb/.git/

If you run git init in a folder that already had files, don’t worry. Git does not delete or modify anything.

It simply activates and waits for you to tell it which files you want to save.

What Really Happened? The Hidden .git Folder

If you go to your folder, it will seem like nothing happened. Your files are still the same. But if you enable the “Show hidden files” option in your operating system, you’ll see that a new folder called .git has appeared.

This folder is actually the repository

All the internal architecture (the Staging Area, the commit database, local configuration, branches…) lives inside this folder.

  • Your visible files are the Working Directory.
  • The .git folder is the Repository.

Technical Structure of a Git Repository

Generally, you don’t need to look at or touch the hidden directory called .git. But, for your information, the structure is something like this:

.git/ ├── HEAD # Points to the current branch
├── config # Repository-specific configuration
├── objects/ # Stores all objects (commits, trees, blobs)
├── refs/ # References to commits (branches and tags)
└── hooks/ # Scripts to automate actions

Never delete or manually modify the .git folder unless you want to delete the entire project history and stop using Git in that folder.

If you delete it, your files will remain, but you will lose all “Save points” (commits) and branches.

Checking the Repository with git status

Now that we have the repository initialized, we can check its status with git status

CommandDescription
git statusShows the current status of the files in your working directory.

To execute it, we simply do:

git status

If you just initialized a repository in a folder with files, you will see something like this:

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        index.html
        style.css
        script.js

nothing added to commit but untracked files present (use "git add" to track)
Copied!

Git is telling us:

  1. On branch main: We are on the main branch.
  2. No commits yet: The repository is newly created, there is no history.
  3. Untracked files: It sees files in the folder, but is not tracking them. If you modify them now, Git won’t know.

This is the natural state of a newly created repository. Git does not track anything automatically; we have to be explicit.

What About git clone?

If you’ve seen some Git before, you’ve probably heard of git clone. What’s the difference?

Characteristicgit initgit clone
UseCreate a new repoCopy an existing repo
OriginLocalRemote (URL)
HistoryEmptyComplete (includes previous commits)
RemotesNone by defaultConfigures origin automatically
  • We use git init when we have a project on our computer (empty or with code) and want to create a new repository locally.
  • We use git clone when the repository already exists on a server (like GitHub or GitLab) and we want to download a copy to our computer.

Technically, git clone does a git init internally, adds the remote server, and downloads all the data. But we’ll see that later when we talk about remote repositories.