Start a Practice Repository

In the last lesson, you installed Git and gave it your name and email, so it’s ready to record your work. Before you point it at the store’s website, it helps to try Git in a practice folder, where nothing is at stake.

In this lesson, you’ll make a small throwaway folder with two files, turn it into a Git repository, and look at what Git created. You’ll also learn how to check which folder a repository covers, so you always know where Git is saving your work. Start with the first thing anyone does in a new folder, which is asking Git what it sees there.

Ask Git About a New Folder

To practice, you need a folder of your own that isn’t the store’s site. Keep it separate from and outside the generic-grocery folder, because the store will get a repository of its own later. Open a terminal and create a folder called grocery-practice in your home folder:

Language: Shell
$ cd ~
$ mkdir grocery-practice
$ cd grocery-practice

Now open grocery-practice in your editor with File → Open Folder… and create two files in it. The first is a tiny script with two of this week’s deal prices, in cents, matching the format in the store’s data:

Language: JavaScript Filename: prices.js
// Practice copy of two deal prices, in cents.
const prices = {
  eggs: 399,
  cranberries: 279,
};

console.log(prices);

The second is a notes file for yourself:

Language: Text Filename: notes.txt
Practice folder for trying out Git.
Nothing in here goes on the store's site.

This is a toy project on purpose. Two small files are enough to see everything Git does. Keep the folder until the end of the course, though, because a later lesson, where you fix the last commit with --amend, comes back to it. After that lesson, you can delete the whole folder without losing anything that matters.

Back in the terminal, still in grocery-practice, ask Git about the folder with git status. This is the command you’ll run more than any other, because it tells you what Git sees right now:

Language: Shell
$ git status
fatal: not a git repository (or any of the parent directories): .git

You’ll see this message in a folder outside any Git repository, and it means your Git installation is working. Git can’t report the folder’s status, but the message tells you exactly why. Git looked for a folder named .git in grocery-practice, then in each folder above it, all the way up, and found none. Without a .git folder, Git doesn’t track anything in this folder.

To change that, run git init in the same folder:

Language: Shell
$ git init
Initialized empty Git repository in /Users/you/grocery-practice/.git/

And… your folder is now a repository, a folder whose history Git keeps. The path in the message depends on your system. On Linux it starts with /home/you, and on Windows it looks like C:/Users/you/grocery-practice/.git/, with forward slashes.

Look Inside the .git Folder

The message mentioned a .git folder, but your editor doesn’t show one. The dot at the start of its name makes it a hidden folder on macOS and Linux, and Git marks it as hidden on Windows. VS Code also leaves .git folders out of its Explorer panel. To see it, list everything in the folder, hidden items included:

Language: Windows PowerShell
PS> Get-ChildItem -Force
Language: Shell
$ ls -a
.  ..  .git  notes.txt  prices.js

There it is, next to your two files. In PowerShell, .git shows an h in its Mode column, which marks it as hidden. The .git folder is the repository itself. It holds your project’s history, every version you’ll save from now on, plus this repository’s own settings. You won’t edit anything in it by hand, because Git’s commands do that for you.

Everything outside .git, here prices.js and notes.txt, is the working tree, the files as they are right now, which you open and edit as usual. You can think of .git as a filing cabinet that sits in the corner of your project folder and holds every saved version. The comparison stops working when you want to look inside, though, because you open the cabinet with Git commands instead of with your hands.

This split matters the day you clean up a folder. If you delete .git, your files stay exactly as they are, but the whole history is gone, and Git keeps no copy of it anywhere else on your computer. Delete the project folder, and the files and the history go together.

To keep your history safe, save a second copy of the repository somewhere else, such as on GitHub. You’ll set that up later on the path. Until then, the .git folder is the only place your history lives.

Want to make sure you know what survives if .git goes away? Open a short quiz in the mentor, answer one question at a time, and get feedback on each answer.

With the repository in place, run git status again and compare its answer with the error from before:

Language: Shell
$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    notes.txt
    prices.js

nothing added to commit but untracked files present (use "git add" to track)

This time Git knows the folder:

  • You’re on the branch main, which you set as the default in the last lesson.
  • There are no commits yet, so no saved versions of your project.
  • Git calls both files untracked because it sees them in the working tree but you haven’t told it to track them yet.

How you tell it is the topic of the next lesson.

Check Which Folder Git Tracks

Real projects have subfolders, like the store’s src/data/, and you’ll run Git commands from inside them. When you’re a few folders down, Git finds the repository you mean with the same search you saw in the first error message, starting in the current folder and moving up until it finds a .git folder.

You can try this with a subfolder. Create one called drafts and step into it:

Language: Shell
$ mkdir drafts
$ cd drafts

The drafts folder has no .git folder of its own. Before you run the next command, think about what git status will answer in here. Will it say “not a git repository” or something else?

Language: Shell
$ git status
Language: Shell
$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    ../notes.txt
    ../prices.js

nothing added to commit but untracked files present (use "git add" to track)

Git didn’t find a .git folder in drafts, so it moved up one level and found the one in grocery-practice. You’re inside that repository, and Git shows the paths relative to where you are, so ../notes.txt means “notes.txt, one folder up.” The empty drafts folder itself isn’t listed, because Git saves files, and a folder shows up only once it has a file in it.

When you want to know for sure which folder a repository covers, ask Git for its top level, the folder that holds the .git folder:

Language: Shell
$ git rev-parse --show-toplevel
/Users/you/grocery-practice

It’s the practice folder, which is what you want. On Windows, the path looks like C:/Users/you/grocery-practice. Now go back up and remove the empty drafts folder, so the practice folder holds only .git and your two files again:

Language: Shell
$ cd ..
$ rmdir drafts

This search upward is also why you run git init inside the project’s own folder and never in your home folder. There, git init would turn every project folder below it that has no .git of its own into one huge repository. If git rev-parse --show-toplevel ever prints your home folder, that’s what happened. That home folder .git is safe to delete only once you’ve checked that it holds no history you want.

Want to practice telling which repository you’re in? Open a short quiz in the mentor about the upward search and the top level, and answer each question before you see the next.

Check Your Understanding

Try answering these on your own before you open the answers:

  1. You create a folder called bakery-menu in your home folder, put a file menu.js in it, and run git status there. Neither bakery-menu nor any folder above it has a .git folder. What does Git answer, and what do you run to make the folder a repository?
  2. You want to send a friend your practice folder without its history, so you delete its .git folder. You’ve never copied the repository anywhere else. What’s left in the folder, and can Git bring the history back?
  3. Your project folder recipe-box has a .git folder, and you’re working in its subfolder recipe-box/images, which has none. You run git rev-parse --show-toplevel there. Which folder does it print, and why?
  1. Git answers fatal: not a git repository (or any of the parent directories): .git, because there’s no .git folder in bakery-menu or in any folder above it. You run git init inside bakery-menu, and git status then lists menu.js as untracked.
  2. The working tree is left, so prices.js, notes.txt, and any other files stay exactly as they were. The history is gone for good, because it lived only in .git, and Git keeps no other copy. Git commands in that folder answer “not a git repository” again.
  3. It prints the path of recipe-box, such as /Users/you/recipe-box. Git found no .git folder in images, so it moved up one level and found the one in recipe-box, which makes recipe-box the top level of the repository you’re in.

Your practice folder is now a repository with an empty history, and git status lists prices.js and notes.txt as untracked. What you want next is to save both files in your first commit. In the next lesson, you’ll try exactly that, and you’ll find out why Git wants one more step from you before it records anything.

Become a Member to join the conversation.