Git works with repositories, often shortened to repos. A Git repository is simply a project folder that Git is tracking. A normal folder contains only your project files, while a Git repository also contains the information Git needs to keep track of versions, branches, commits, and other history.
There are two common ways to start working with a Git repository. If you have created a new project on your computer and want Git to start tracking it, you use git init. If the project already exists on GitHub and you want to download it to your computer, you normally use git clone.
Option 1: Create a New Repository with git init
Suppose you already have a project folder called:
my-projectOpen a terminal, move into that folder, and run:
git initIf the command succeeds, Git will print a message similar to:
Initialized empty Git repository in ...At this point, the ordinary folder has become a Git repository. Git creates a hidden folder called .git inside the project. This folder stores the internal information Git needs to manage the repository, including its history, configuration, and branches.
You normally do not need to open or edit the .git folder yourself. For a beginner, the important thing to know is simple: if a project contains a .git folder, it is usually a Git repository.
Checking the Repository with git status
Once you are inside a Git repository, one of the most useful commands is:
git statusThis command tells you what is currently happening in the repository. For example, if you create a new file called:
hello.txtand then run:
git statusGit may show something like:
Untracked files:
hello.txtThe word untracked means that Git can see the file, but the file has not yet been added to the repository's version history. These different file states are explained later in the course. For now, just remember that whenever you are unsure what Git thinks is happening, git status is usually a good command to run first.
Option 2: Get an Existing Repository with git clone
In real development work, you will often join a project that already exists on GitHub. Instead of creating a new repository, you can copy the existing repository to your computer with:
git clone <repository-url>For example:
git clone https://github.com/example/demo-project.gitGit will download the repository and create a new project folder in your current directory:
demo-project/Unlike downloading a ZIP file, git clone does more than copy the current project files. It also downloads the Git repository information and history. This means the cloned project is already a Git repository, so you do not need to run:
git initIn other words, you normally do not run git clone and then git init on the same project. If a repository already exists on GitHub, cloning it is enough.
KNOWLEDGE QUIZ
When Should You Use git init or git clone?
The difference is straightforward. If you have a new local project and want Git to start tracking it, use:
git initIf the project already exists on GitHub and you want a local copy, use:
git clone <repository-url>A simple way to remember it is:
- New local project →
git init - Existing GitHub project →
git clone
For example, if you are starting a small practice project from scratch, you can create the folder and run git init. If a teammate sends you a GitHub repository link for an existing project, you can use git clone instead.
A Simple Example
To create a new project locally, you might run:
mkdir git-demo
cd git-demo
git initThen check its status:
git statusYou now have a Git repository, although it does not contain any commits yet.
If the project already exists on GitHub, the process is even simpler:
git clone https://github.com/example/git-demo.gitThen enter the project folder:
cd git-demoAnd check its status:
git statusWhether you use git init or git clone, the end result is the same: you have a local repository that Git can manage.
Practice
Write complete commands for two scenarios:
A. create a git-demo folder and initialize it as a repository;
B. obtain https://github.com/example/git-demo.git and enter the project.