Creating Repositories
Getting Started
Let's start using Git! There are 2 common starting points:
- Introduce Git before you even start coding. In this case, you'll create an empty Git repository ready to hold all of your brand-new project files.
- You already have a project in progress where you'd like to introduce version control. In this scenario, you'll transform an existing directory full of files into a Git directory.
Create an Empty Git Repository
First, create an empty directory on your computer where you'd like to store your project files.
mkdir my-projectNext, navigate into that new empty directory
cd my-projectCreate the Git repository
git initNote: These terminal commands may look different depending on if you're using Linux/macOS or Windows. In this learning track all commands are written for Linux/macOS
A note about terminology
In the above example, we created a new directory on our computer, then created a Git repository from the directory. The terms "initialize a Git repository" and "create a Git repository" are equivalent. They mean we are creating the necessary components for Git to do its job: the snapshot database, staging area, and hidden .git folder that Git uses for internal tracking.
Add Git to an Existing Directory
If you have an existing project that is not currently being version controlled, use the following steps to start version-controlling it with Git.
Navigate to the existing directory that holds your project files
cd /my/existing/project/directoryInitialize Git in your project directory
git initWhen initializing a repository, Git does not automatically snapshot existing files in the directory. This means it will not track file changes until you tell Git to snapshot and start tracking changes to files. Do this by adding them to the staging area and committing.
git add .git commit -m 'Initial commit'Don't worry if the add and commit commands don't make sense yet; we'll cover them in depth in the next lesson.
Clone a Git Repository
There is a sneaky third way to start your Git projects: duplicating a Git-tracked project that already exists. This is possible because Git stores versions of all project files in the repository. Simply grabbing a copy of the Git repository is enough to rebuild an entire directory of project files and folders.
Cloning is beyond the scope of this learning track, but if you're curious to dive deeper, check out the official documentation.