Skip to content

Creating a Repository

An ordinary project directory holds application files, but Git has no local history for it yet. Initializing a repository gives Git a place to maintain repository data without changing the project’s existing files.

backend-api/ project directory and working files
app/
tests/
.git/ local Git repository data

The .git directory is Git’s own storage and configuration area. It is not a project file that Git tracks. A directory can contain hundreds of source files and still have no recorded Git history. Initialization alone neither records those files nor creates a commit.

Git works locally. This command does not create a GitHub repository or send anything over the network.

Check that you are in the intended project directory, then run:

Terminal window
pwd
git init

For a new repository, Git reports a path ending in .git/:

Initialized empty Git repository in .../backend-api/.git/

The existing application files remain where they were. They have not been recorded in a commit merely because the repository now exists.

From its parent directory, you can provide a directory name:

Terminal window
git init backend-api

If backend-api does not exist, Git creates it and initializes a repository inside it. If it already exists, Git initializes it in place without deleting its files. Quote a path containing spaces:

Terminal window
git init "Backend API"

Running git init again in an existing repository normally reports:

Reinitialized existing Git repository in .../backend-api/.git/

It does not erase the project’s files or existing commits. In a disposable exercise, two consecutive runs returned Initialized empty Git repository and then Reinitialized existing Git repository for the same .git path. In another exercise, an existing commit remained visible after reinitialization.

Reinitializing is not a repair command to run blindly. If you expected a different repository, first check your current directory; otherwise you may initialize the wrong project.

5. The initial branch before the first commit

Section titled “5. The initial branch before the first commit”

Git chooses an initial branch name when it initializes the repository. The name may depend on your init.defaultBranch configuration or your Git setup. Before the first commit, that branch does not point to a commit yet. This is called an unborn branch. It does not mean initialization failed: the repository exists, but its history is still empty.

Suppose you already have a Laravel application. Initializing its directory adds Git’s local repository data but does not automatically record the application files. Confirm the intended location before running the command, particularly when several projects are open in different terminals. Practice with a disposable directory rather than an active client project.

Common mistakes include treating .git as an application folder to be tracked, assuming initialization saves every file, assuming it connects to GitHub, and believing a named initial branch already has a commit.

ordinary directory + git init → directory with local Git repository data
git init <directory> → create or initialize the named directory
second git init → reinitialize the existing repository
before first commit → initial branch has no commit yet
Does git init record the files already in the directory?

No. It creates repository data; initialization alone does not create a commit.

What happens when you run git init a second time?

Git reinitializes the existing repository without deleting its files or existing commits.

What does “unborn branch” mean?

The initial branch has a name but does not point to a commit yet.