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.
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.
Make sure that the terminal is currently in the root directory of the project.
-
HINTUse the
pwdcommand in the VS Code terminal to check the current working directory. -
HINTUse the
cdcommand to navigate to the desired directory if needed.<path> -
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:
git initRun 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/.gitThis 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). 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 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.
Next step