Skip to content
$ lynuxpro
← Git

LESSON 3 OF 21 · BEGINNER · 16 MIN

Your first repository

HOW IT WORKSinit, add, commit

git initcreates .git/
1/5

git init turns a folder into a repository by creating a hidden .git folder that will hold the history.

$ git init

Time to use Git for real. In this lesson you create a repository, add files, make commits and learn to read git status, the command you will run more than any other. Follow along in the practice terminal: it runs a real Git model, so every command behaves like it does on your machine.

In simple words

  • git init: turns a folder into a Git repository.
  • Working tree: the files you see and edit.
  • Staging area: a waiting area where you choose which changes go into the next commit (git add).
  • Commit message: a short line saying what changed and why. Good messages make history easy to read.

Set up once

If you have not done it in this terminal yet, set your identity:

git config --global user.name "Leela Kumar"git config --global user.email "leela@acme.dev"
SHELLbashutf-8 · 2L

Create a repository

git init turns the current folder into a repository by creating a hidden .git folder, where Git stores the history:

mkdir shopcd shopgit initls -a
SHELLbashutf-8 · 4L
Initialized empty Git repository in /home/lynuxpro/shop/.git/.  ..  .git
OUTPUTterminalutf-8 · 2L

Never edit files inside .git by hand. Deleting it deletes the whole history.

The other way to get a repository is git clone URL, which copies an existing one from a server. You will do that in the remotes lesson.

The three states of a file

Every file in your project is in one of these states:

  • Untracked: new; Git sees it but is not recording it.
  • Staged: marked to go into the next commit.
  • Committed (unmodified): safely stored in the history, unchanged since.
  • Modified: tracked, but changed since the last commit and not yet staged.

git status tells you which state everything is in. Create two files and look:

printf '# Shop\n\nA small checkout service.\n' > README.mdprintf 'print("Shop is running")\n' > app.pygit status
SHELLbashutf-8 · 3L
On branch mainNo commits yetUntracked files:  (use "git add <file>..." to include in what will be committed)	README.md	app.pynothing added to commit but untracked files present (use "git add" to track)
OUTPUTterminalutf-8 · 10L

Git even tells you the next command to run. Read git status carefully; it answers most "what is going on?" questions.

Staging with git add

git add puts a snapshot of a file into the staging area (also called the index): the next commit, being prepared.

git add README.mdgit status
SHELLbashutf-8 · 2L
On branch mainNo commits yetChanges to be committed:  (use "git restore --staged <file>..." to unstage)	new file:   README.mdUntracked files:  (use "git add <file>..." to include in what will be committed)	app.py
OUTPUTterminalutf-8 · 11L

Why have a staging area at all? It lets you choose exactly what goes into each commit. If you fixed a bug and also tidied an unrelated file, you can commit them separately, which makes history much easier to read and to revert.

Ways to stage:

CommandStages
git add file1 file2specific files
git add src/a whole folder
git add .everything in the current folder and below
git add -Aevery change in the repository, including deletions
git add -pchosen parts of files, hunk by hunk (interactive)

Committing

git commit saves everything that is staged as a new snapshot:

git commit -m "Add README"
SHELLbashutf-8 · 1L
[main (root-commit) 4a1c2e9] Add README 1 file changed, 3 insertions(+) create mode 100644 README.md
OUTPUTterminalutf-8 · 3L
  • main is the branch, 4a1c2e9 the start of the commit's unique ID (its hash). Your hash will differ.
  • root-commit means it is the first commit, with no parent.
  • Only the staged file was committed. app.py is still untracked.

Add and commit the app too, then check the status:

git add app.pygit commit -m "Add app entry point"git status
SHELLbashutf-8 · 3L
[main 8f3b0d1] Add app entry point 1 file changed, 1 insertion(+) create mode 100644 app.pyOn branch mainnothing to commit, working tree clean
OUTPUTterminalutf-8 · 5L

"Working tree clean" means your files exactly match the last commit.

Modify, stage, commit

The everyday loop is: edit, check status, stage, commit.

printf 'print("Shop is running on port 8080")\n' > app.pygit status -sgit add app.pygit status -sgit commit -m "Show the port on start-up"
SHELLbashutf-8 · 5L
 M app.pyM  app.py[main 2c7d9e4] Show the port on start-up 1 file changed, 1 insertion(+), 1 deletion(-)
OUTPUTterminalutf-8 · 4L

git status -s is the short form: the left column is the staging area and the right column is the working tree. M (M on the right) means modified but not staged; M (M on the left) means staged. ?? means untracked and A means a new file is staged.

For files Git already tracks, git commit -am "message" stages all modifications and commits in one step. It never adds untracked files.

Look at what you made

git log --oneline
SHELLbashutf-8 · 1L
2c7d9e4 (HEAD -> main) Show the port on start-up8f3b0d1 Add app entry point4a1c2e9 Add README
OUTPUTterminalutf-8 · 3L

Newest first. HEAD -> main means you are on the main branch, at its latest commit. The next lesson goes deep into history.

Writing good commit messages

Commit messages are read far more often than they are written: during code review, debugging and incident response. A widely used convention:

Short summary in the imperative, under ~50 charactersOptional body after a blank line, wrapped at ~72 characters. Explainwhat changed and why, not how: the diff already shows how. Mentionticket numbers like SHOP-142 if your team uses them.
OUTPUTterminalutf-8 · 5L
  • Imperative mood: "Add retry to payment client", as if completing "this commit will…". Not "added" or "adding".
  • One logical change per commit: easier to review and to revert.
  • Many teams use Conventional Commits prefixes (feat:, fix:, docs:, chore:), which tools use to generate changelogs and version numbers.

Bad: fix, wip, changes, asdf. Good: Fix rounding error in GST calculation.

Real-world scenario: starting infrastructure as code

A team that managed servers by hand decides to put their configuration in Git. The first step is exactly what you just did: git init in a folder with their nginx config and deployment scripts, git add the files, and a first commit "Import current production nginx config". From that moment every change has an author, a reason and a way back, and the next step is to push it to GitLab and let a pipeline deploy it.

Common mistakes

  • Running git init in your home folder, which turns everything you own into one giant repository. Always init inside a project folder.
  • Committing everything with git add . without looking, including logs, build output or .env secrets. Check git status first, and set up .gitignore (lesson 6).
  • Huge commits mixing unrelated changes.
  • Meaningless messages that make history useless.
  • Forgetting that -am skips new files.

Practice

  1. Create a repository called notes, add a todo.md file and commit it.
  2. Modify todo.md, run git status -s before and after git add, and explain the columns.
  3. Create two new files and commit them in two separate commits.
  4. View your history with git log --oneline.

Cheat sheet

git init                     start a repository heregit status  /  git status -s what changed (long / short)git add file | dir | .       stage changesgit add -p                   stage parts of filesgit commit -m "message"      commit staged changesgit commit -am "message"     stage tracked changes + commitgit log --oneline            short history
OUTPUTterminalutf-8 · 7L

Key takeaways

  • git init creates a repository; the history lives in .git.
  • Files move from untracked or modified, to staged with git add, to committed with git commit.
  • git status (and git status -s) shows exactly where every file stands and suggests the next command.
  • The staging area lets you build focused commits.
  • Write commit messages in the imperative that explain why the change was made.