3. Putting the project under version control

Initializing a repository

We're about to initialize our first repository! I hope you didn't close the terminal yet, because we're going to use it soon. Before we actually create the repository, I'd like to explain something very important about working in the terminal.

Current working directory

Each terminal tab is its own command line session, and you can keep several parallel sessions open side by side.

Every terminal session begins inside a specific directory on your computer. That location is called the current working directory (commonly abbreviated as cwd).

A directory is the same thing as a folder. Different operating systems use different terms, but they mean the same thing.

While working in the terminal, many commands will operate on files and directories relative to the current working directory. Naturally, you can navigate to different directories as needed.

Most terminals start in the user's home directory by default, but it can be changed to any directory. In VS Code, a fresh terminal typically opens right inside the directory you have opened as your workspace, so you're usually in the right place from the start.

To check your current directory in the terminal, you can run the pwd command. It will show you the full path to the current directory.

If you want to change the current directory, you can use the cd command. For example, to navigate to the root directory of the project (root means top level directory), you can run the following command:

cd /home/your-user-name/gitbybit

Gotchas about paths

Note that if the path contains spaces, you need to enclose it in quotes so that the cd command treats it as a single argument.

For example, if the path is C:\Program Files, you would run the command cd "C:\Program Files". You can use both single ' and double quotes ", but always use the same pair of quotes.

For example, if the path is /home/alex/my documents, you would run the command cd "/home/alex/my documents". You can use both single ' and double quotes ", but always use the same pair of quotes.

Also, note that on most operating systems (Linux and macOS included), paths and commands are case-sensitive. For example, /home/Alex and /home/alex are two different paths.

Absolute paths

This: /home/your-user-name/gitbybit is an absolute path, a path that starts from the root directory of the file system. Such paths are super specific and always point to the same location on your computer (e.g. /home/alex/gitbybit-part1/file.txt), regardless of your current working directory. However, they are specific to YOUR computer, so they are not that useful in instructions meant for other people.

Absolute paths always start with a drive letter (such as C: on Windows) or a slash character (/, which means the root of the file system on Linux and macOS).

Relative paths

There are also relative paths, which are relative to a current working directory. They are more flexible and portable. For example, file.txt or ./file.txt refers to a file in the current directory, and ../notes/todo.md points to a file inside the notes folder one level up.

Because relative paths are based on the project's folder structure rather than your computer's root directory, the same commands will work on any machine—as long as everyone has the project organized the same way. This makes them ideal for shared projects, version control, and documentation that others can follow without changing paths.

There are special shortcuts like . (current directory) and .. (parent directory). So, to navigate to the parent directory, you can use the cd .. command. This command moves you one level up in the directory tree.

Sometimes you can see paths like this: ./example. That means the same as example (the current directory is implied). So, the cd ./example command is equivalent to cd example.

You can combine these shortcuts with directory names to navigate to a specific directory. For example, to navigate to the example directory two levels higher than your project, you would run the cd ../../example command.

Many command line tools accept paths as arguments, so you can use this knowledge beyond just the cd command.

SUPER HELPFUL TIP: You can use the Tab key on your keyboard to autocomplete file and directory names in the terminal. For example, if you have a src subdirectory, you can type cd s and then press Tab to autocomplete the command to cd src (or to cd ./src on some systems, which is the same). This is a great way to avoid typos and speed up your work.

Tab completion in the Terminal.
Great, nice to know

Sorry for such a knowledge bomb

...but understanding paths and the current working directory is absolutely crucial when working in the terminal.


Initializing the repository

When you open a new terminal in VS Code, it usually starts in the workspace directory you opened for this course, the project root. This is exactly where we want to create our repository. But it never hurts to double check that you're in the right place.

While you were away getting some coffee, a cat decided to have a nap on your keyboard.

Task

Make sure that the terminal is currently in the root directory of the project.

  • HINTUse the pwd command in the VS Code terminal to check the current working directory.
  • HINTUse the cd <path> command to navigate to the desired directory if needed.
  • HINTThe project root directory is /home/your-user-name/gitbybit.

Great, we can now initialize the repository. The command to create a new repository is git init. This command creates a new empty repository in the current directory. Run the following command in your terminal to initialize the new repository:

Run in Terminal:
git init
Task

Run the git init command in the VS Code terminal.

If everything went according to plan, you should see something like this as an output of the command:

Result:
Initialized empty Git repository in /home/your-user-name/gitbybit/.git

This means that changes in our project can now be tracked by Git.

Anything else?

The repository itself is located in a hidden directory named .git. Because it's hidden, you won't see it in the file explorer by default. However, you can see it in the terminal by running the ls command with the -Force-a option. Keep in mind that if something happens to the .git directory, the repository will be corrupted.

Everything else in the root directory of the project is usually called the working tree (or the working directory). Think of the directories and files in your project branching out like limbs on a tree—that's why Git uses the word tree here. It's the place where you do all the work, create and edit files, etc. The files in the working tree can differ from what you saved in the repository. For example, you can have a file in the working tree that is not yet in the repository. You can tell Git to either save the changes in the repository or discard them.

By the way, you might have noticed some changes in the VS Code interface after we initialized the repository. The SOURCE CONTROL panel is now connected to our new repository. In the corner, you can also see the name of the current branch (we'll talk about branches later).

git init in VS Code

In fact, the SOURCE CONTROL panel is a graphical interface to the Git repository. For the sake of practice, in this course we will only be using the terminal to interact with the repository. But in real life, many common operations, such as adding or committing files, can be done faster using the graphical interface.

Next step
© 2024-2025 GitByBit.All rights reserved.