
Creating your first Git project
In this recipe, we will take a look at creating your first Git project on your local machine. If you're a Windows user, please make sure that you use Git Bash. This way, you can use the same commands as the ones Linux and OS X users use.
We're going to create a project called super-git
.
How to do it…
To create our first Git project, perform the following steps:
- Open your terminal and browse to the folder where you want to create your project.
- Create a folder named
super-git
and create the following directory inside it:$ mkdir super-git $ cd super-git
- To make this folder a Git project, we need to tell Git to monitor this folder. We do this by typing the following command:
$ git init
The following screenshot shows you the output for this command:
- Let's create one file to add to the repository. We name it
README.md
:$ echo "HELLO README" > README.md
- Now, we will add this new file to Git. This is also known as staging the files:
$ git add README.md
- Commit the file to our local repository:
$ git commit -m "Our first commit"
The following screenshot shows you the output of this command:
How it works…
We just created our first Git project called super-git
, created a file, and committed that file to the repository.
Let's go through the steps we took, and I'll explain a little more about what we did.
Every Git project is just a folder that is tracked by Git. This is why we needed to first create a folder where we wanted to store our project. After we created the folder, we ran a git init
command. This command creates a .Git
folder with some subdirectories for Git to track your changes. The Git init
command can be run as many times as you want. It will just create the Git project once.
Next was the creation of a file. As Git does not track empty folders, we had to create a file so that the repository was not empty anymore. Every project benefits from a good README
file, so this is the first file that we'll create.
Before you can commit a file, you need to tell Git that you want to stage that particular file. Staging a file means that you want Git to commit that file on the next commit. In our case, we just told Git that we wanted to stage one file, but if you have a lot of changes, and want to stage them all, you can stage them using the git add --all
command.
We had to tell Git to commit the file that we staged to our local repository, and we did this by performing the git commit –m "your commit message"
command. The –m
parameter is for the commit message. If you want to enter a longer commit message, you can do so by only typing git commit
. This will open your editor, and you can change your commit message this way.