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.
-
Each time you open a new terminal, you start a new command line session. This session starts in a specific directory on your computer, called the current directory. The sessions are independent of each other, so you can open several terminals at once, and each of them will have its own current directory.
-
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
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 /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 every operating system (except Windows), paths and commands are case-sensitive. For example, /home/Alex
and /home/alex
are two different paths.
-
In the previous example, I used the full directory path (meaning, a path that starts from the root directory of the file system), but you can also use relative paths.
Suppose you have a
src
subdirectory in your project, and you want to navigate to it from the project root. To do that, you would run thecd src
command. The path doesn't start from a drive letter (such asC:
on Windows) or a slash character (/
, which means the root of the file system on Linux and macOS), which means it's a relative path. Generally, it's relative to the directory you are currently in. -
There are also special shortcuts like
.
(current directory) and..
(parent directory). So, to navigate to the parent directory, you can use thecd ..
command. This command moves you one level up in the directory tree. -
Sometimes you can see paths like this:
./example
. That means the same asexample
(the current directory is implied). So, thecd ./example
command is equivalent tocd 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 thecd ../../example
command. -
Many command line tools accept paths as arguments, so you can use this knowledge beyond just the
cd
command.
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.When you open a new terminal in VS Code, it usually starts in the root directory of the project. This is exactly where we want to create our repository. But it never hurts to double check that you're in the right place.
Run the pwd
command in the VS Code terminal. Make sure you're in the root directory of the project. If not, navigate to it using the cd /home/your-user-name/gitbybit
command.
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:
git init
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:
Initialized empty Git repository in /home/your-user-name/gitbybit/.git
This means that changes in our project can now be tracked by Git.
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 -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). It's a 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 CodeIn 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.
Hi! I'm Alex, creator of GitByBit.
This page is a part of the interactive course about Git version control.
It's a one-of-a-kind course that is integrated into the VS Code code editor. Learning directly in VS Code lets you operate Git exactly as you would in real life, doing real jobs, writing real code.
In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.