A Git repository is a project directory that Git tracks for version control. You can create a new repository inside an existing project folder using the git init command.
git init command initializes
a Git repository in the current directory. It does not automatically
create a commit or upload your project to GitHub.
A Git repository is a directory that contains your project files and Git's version control information.
The repository allows Git to track changes and maintain project history.
The git init command creates the Git metadata needed to start tracking a project.
git init
After running the command, the current directory becomes a Git repository.
First, create a project directory.
mkdir my-project
This creates a folder named my-project.
Use the cd command to enter the project directory.
cd my-project
Git commands such as git init will then operate in this
directory.
Run the following command inside your project folder:
git init
Git initializes the current directory as a repository.
When you run git init, Git creates a hidden directory
named .git.
This directory contains Git's repository metadata and is essential to the repository.
The .git directory stores information that Git uses to manage the repository, including objects, references and configuration.
Deleting this directory removes the repository metadata from that project directory.
After initializing a repository, use:
git status
This displays the current state of the repository.
my-project/
.git/
index.html
style.css
script.js
The .git directory contains Git's internal repository information while the other files are project files.
You do not have to create a new project folder before using Git. You can initialize Git inside an existing project.
cd my-existing-project
git init
Git will begin managing the project as a repository.
Running git init does not automatically create commits
for all project files.
You need to stage changes and create commits:
git add .
git commit -m "Initial commit"
Create a file after initializing the repository:
touch index.html
git status
Git will normally show the new file as an untracked file.
Use git add to stage a file for the next commit.
git add index.html
The file is now included in the staging area.
To stage all changes in the current directory and its subdirectories, you can use:
git add .
Review the staged changes with git status before committing.
After staging your project files, create your first commit:
git commit -m "Initial commit"
This records the staged changes in the repository history.
You can view the repository history using:
git log
The output includes information about the commit, such as its identifier, author, date and message.
You can specify the initial branch name while initializing a repository. For example:
git init -b main
This initializes the repository with main as its initial branch name.
The simplest form of the command initializes the current directory:
git init
Make sure you are inside the intended project directory before running the command.
Git can also initialize a repository in a specified directory:
git init my-project
This creates or initializes the repository in the specified project directory.
A local Git repository does not automatically connect to GitHub. You can later add a GitHub repository as a remote.
git remote add origin https://github.com/username/my-project.git
The remote connection will be covered in a later lesson.
A Git repository can work entirely on your local computer. You can create commits, branches and inspect history without connecting the repository to GitHub.
git init
git add .
git commit -m "Initial commit"
git log
Git can show the root directory of the current working tree with:
git rev-parse --show-toplevel
This can be useful when working with nested directories.
Create Project
↓
Enter Project Folder
↓
git init
↓
Create or Modify Files
↓
git status
↓
git add .
↓
git commit
This is a basic local Git workflow.
mkdir website
cd website
touch index.html
touch style.css
git init
git status
This creates a simple website project and initializes it as a Git repository.
git add index.html
git add style.css
You can stage files individually when you want precise control over what will be included in the next commit.
git commit -m "Create website structure"
This creates a commit containing the staged website files.
Afterward, use git log to inspect the commit history.
git init in the wrong foldergit init creates a commitgit init uploads files to GitHub.git directory accidentallygit init
git status
git add .
git commit -m "Initial commit"
git log
git branch
These commands provide a simple starting point for working with a newly initialized repository.
mkdir student-project
cd student-project
touch index.html
git init
git status
git add index.html
git commit -m "Initial student project"
git log
This example creates a project, initializes Git, stages a file, creates a commit and displays the history.
The git init command is used to create a new Git repository. It creates the .git directory that contains Git's repository information.
After initialization, you can add files, create commits, create branches and eventually connect the repository to a remote service such as GitHub.
git init
git status
git add .
git commit -m "Initial commit"
In the next lesson, we will learn about the structure of a Git repository and understand the role of the .git directory.
Question: Which command is used to initialize a new Git repository?