A Git repository is where your project’s files and version history are stored. This page covers how to initialize and manage repositories in Git.
A repository (repo) is a folder that Git uses to track changes to files. It can be:
- Local: Stored on your computer.
- Remote: Hosted on a server like GitHub, GitLab, or Bitbucket for collaboration.
-
Navigate to the directory where you want to create the repository:
cd /path/to/your/project
-
Initialize Git in the directory:
git init
This creates a hidden
.git
folder where Git stores metadata about your repository. -
(Optional) Add files to the repository:
git add .
-
Commit the files to create a snapshot:
git commit -m "Initial commit"
The directory is now a Git repository, and Git will track changes to files in this folder.
If a repository already exists, you can clone it to your local system.
- Copy the repository URL (e.g., from GitHub or GitLab).
- Run the
git clone
command:git clone https://github.com/username/repository.git
- Navigate into the cloned repository:
cd repository
You now have a local copy of the repository with its full history.
After initializing or cloning a repository, check its status to see untracked or modified files:
git status
graph LR
A[Working Directory] -->|git add| B[Staging Area]
B -->|git commit| C[Local Repository]
C -->|git push| D[Remote Repository]
D -->|git pull| A
- Working Directory: The folder containing your project files.
- Staging Area: Tracks changes you've marked for commit.
- Git Directory: The
.git
folder that contains the repository’s history and configuration.
You can view the contents of the .git
directory (optional):
ls -a .git
If you’ve initialized a local repository, you can connect it to a remote for collaboration.
-
Add the remote repository URL:
git remote add origin https://github.com/username/repository.git
-
Push the local repository to the remote:
git push -u origin main
Your local changes will now sync with the remote repository.
Simply delete the .git
folder:
rm -rf .git
This action uninitializes the repository and removes its history.
Command | Description |
---|---|
git init |
Initialize a new Git repository. |
git clone <url> |
Clone an existing repository. |
git remote add origin <url> |
Add a remote repository. |
git status |
Check the status of the repository. |
git add <file> |
Stage changes for commit. |
git commit -m "message" |
Commit changes with a message. |
Initializing a Git repository is the first step to managing your project with version control. Whether starting fresh or cloning an existing repository, Git provides a flexible and powerful way to track changes and collaborate.
Next Steps: Tracking Changes