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.
1. The mental model
Section titled “1. The mental model”backend-api/ project directory and working files app/ tests/ .git/ local Git repository dataThe .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.
2. Initialize the current directory
Section titled “2. Initialize the current directory”Check that you are in the intended project directory, then run:
pwdgit initFor 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.
3. Initialize a named directory
Section titled “3. Initialize a named directory”From its parent directory, you can provide a directory name:
git init backend-apiIf 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:
git init "Backend API"4. Reinitialization is not a new history
Section titled “4. Reinitialization is not a new history”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.
6. Backend example and safety
Section titled “6. Backend example and safety”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.
7. Summary
Section titled “7. Summary”ordinary directory + git init → directory with local Git repository datagit init <directory> → create or initialize the named directorysecond git init → reinitialize the existing repositorybefore first commit → initial branch has no commit yet8. Knowledge check
Section titled “8. Knowledge check”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.