Git First Repository
A repository is a folder that Git watches. In this lesson you create one and make your first commits.
Create a Repository
Make a folder for a small website and turn it into a repository:
Example
mkdir my-site
cd my-site
git initInitialized empty Git repository in /home/ali/my-site/.git/
Git creates a hidden .git folder. It holds the whole history. Never edit or delete it by hand.
The Three Areas
Git has three places where your files can be:
| Area | What it is |
|---|---|
| Working directory | Your normal files, as you edit them |
| Staging area | The changes you have chosen for the next commit |
| Repository | The saved commits (history) |
The usual flow is: edit files, stage the changes with git add, then commit them with git commit.
Think of the staging area like a shopping basket: you pick which changes go in, then you pay (commit) for all of them together.
git status
Create a file index.html with any text, then run:
Example
git statusOn 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)
"Untracked" means Git sees the file but is not saving it yet. Run git status often; it always tells you what is going on.
git add
Example
git add index.html
git statusOn branch main No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: index.html
| Command | Stages |
|---|---|
git add index.html | One file |
git add css/ | Everything in a folder |
git add . | All changes in the current folder |
git commit
A commit saves the staged changes with a message that explains them:
Example
git commit -m "Add home page"[main (root-commit) 3f2a9c1] Add home page 1 file changed, 1 insertion(+) create mode 100644 index.html
3f2a9c1 is the start of the commit's unique ID (a hash). Every commit has one.
Making More Commits
Add a stylesheet and change the page:
Example
echo "body { font-family: Arial; }" > style.css
git add .
git commit -m "Add basic stylesheet"[main 8b41d07] Add basic stylesheet 1 file changed, 1 insertion(+) create mode 100644 style.css
For files Git already tracks, you can stage and commit in one step:
Example
git commit -am "Update home page heading"The -a flag stages all changed tracked files. It does not add new files.
Good Commit Messages
- Short (about 50 characters) and clear
- Say what the commit does: "Add contact form", "Fix menu on mobile"
- One commit for one logical change
Bad messages like "changes", "update" or "asdf" make history useless later.
Exercise
Exercise
What does git add do?
