๐๏ธ Creating Repositories โ
Two Ways to Create a Repository โ
There are two ways to create a Git repository, just like there are two ways to build a house โ build from scratch, or buy an existing one (and renovate it).
Method 1: git init โ Building from Scratch โ
# Create project directory
mkdir my-first-website
cd my-first-website
# Initialize Git repository
git init
Initialized empty Git repository in /home/yourname/my-first-website/.git/
# See what happened
ls -la
total 12
drwxr-xr-x 3 yourname yourname 4096 Jun 20 10:00 .
drwxr-xr-x 15 yourname yourname 4096 Jun 20 10:00 ..
drwxr-xr-x 7 yourname yourname 4096 Jun 20 10:00 .gitThat simple! git init creates a hidden .git folder in the current directory. This folder is Git's "brain" โ all version information is stored inside.
Method 2: git clone โ Standing on the Shoulders of Giants โ
# Clone using SSH
git clone git@github.com:yourname/my-first-website.git
Cloning into 'my-first-website'...
remote: Enumerating objects: 42, done.
remote: Counting objects: 100% (42/42), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 42 (delta 12), reused 42 (delta 12)
Receiving objects: 100% (42/42), 8.50 KiB | 8.50 MiB/s, done.
Resolving deltas: 100% (12/12), done.
# Clone using HTTPS (recommended for beginners)
git clone https://github.com/yourname/my-first-website.git
# Clone into a specified directory
git clone https://github.com/yourname/my-first-website.git my-websiteExploring the .git Directory Structure โ
This .git folder is Git's secret base. Let's see what's inside:
ls -la .git/
total 32
drwxr-xr-x 7 yourname yourname 4096 Jun 20 10:00 .
drwxr-xr-x 3 yourname yourname 4096 Jun 20 10:00 ..
-rw-r--r-- 1 yourname yourname 23 Jun 20 10:00 HEAD
drwxr-xr-x 2 yourname yourname 4096 Jun 20 10:00 branches
-rw-r--r-- 1 yourname yourname 92 Jun 20 10:00 config
-rw-r--r-- 1 yourname yourname 73 Jun 20 10:00 description
drwxr-xr-x 2 yourname yourname 4096 Jun 20 10:00 hooks
drwxr-xr-x 2 yourname yourname 4096 Jun 20 10:00 info
drwxr-xr-x 4 yourname yourname 4096 Jun 20 10:00 objects
drwxr-xr-x 4 yourname yourname 4096 Jun 20 10:00 refs| File/Directory | Purpose | Plain English |
|---|---|---|
| HEAD | Points to the current branch | "Which branch am I on?" |
| config | Repository-level configuration | This repo's "personal settings" |
| objects/ | All data (commits, files, trees) | The repo's "memory palace" |
| refs/ | Pointers to branches and tags | Bookmark locations |
| hooks/ | Hook scripts | "If event XX happens, automatically run YY" |
| index | Staging area | "Shopping cart waiting for checkout" |
โ ๏ธ Warning: Never manually modify files in the .git directory! Unless you know exactly what you're doing, your repository could "self-destruct." All operations should be done through Git commands.
First File, First Commit โ
# Create an index.html file
cat > index.html << 'EOF'
<!DOCTYPE html>
<html>
<head><title>My First Website</title></head>
<body>
<h1>Hello, Git!</h1>
</body>
</html>
EOF
# See Git's reaction
git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
nothing added to commit but untracked files present (use "git add" to track)
# Add to staging area
git add index.html
# Commit!
git commit -m "feat: create homepage, Hello Git!"
[main (root-commit) a1b2c3d] feat: create homepage, Hello Git!
1 file changed, 7 insertions(+)
create mode 100644 index.html๐ก Tip: Congratulations! You just made your first Git commit!
a1b2c3dis this commit's "ID number" (the first 7 characters of the hash). From now on, Git will faithfully record every modification you make to this project.