In this tutorial, we will walk through the process of creating and initializing your first Git repository. A Git repository is a virtual storage of your project. It allows you to save versions of your code, which you can access when needed.
By the end of this tutorial, you will be able to:
You should have a basic understanding of using the terminal or command line.
Before you can create a Git repository, you need to have Git installed on your computer. If you have not installed Git, you can download it from the official Git website.
Once Git is installed, you can create a new repository. Open your terminal, navigate to the folder where you want your project to reside, and then type git init
. This will create a new Git repository in your current folder.
$ cd /path/to/your/folder
$ git init
Now you can start tracking changes. Create a new file in the repository folder, add some code, and then save the file. After saving, you can add the file to your repository using the git add
command. This tells Git to include the changes in the next commit.
$ echo "print('Hello, World!')" > hello.py
$ git add hello.py
After adding the file, you can commit your changes with git commit
. This saves your changes to the repository.
$ git commit -m "My first commit"
This code snippet shows how to initialize a Git repository.
$ cd /path/to/your/folder # navigate to your project folder
$ git init # initialize the Git repository
This code snippet shows how to create a file, add it to the repository, and make a commit.
$ echo "print('Hello, World!')" > hello.py # create a new Python file
$ git add hello.py # add the file to the repository
$ git commit -m "My first commit" # commit the changes
In this tutorial, we've learned how to install Git, create a Git repository, and make a commit. With these skills, you can start tracking changes to your projects and collaborate with others.
Now it's time to practice what you've learned. Try these exercises:
goodbye.py
. In this file, write a Python script that prints "Goodbye, World!".goodbye.py
to the repository and make a commit with the message "Goodbye commit".Here are the solutions:
$ cd /path/to/your/other/folder # navigate to another project folder
$ git init # initialize another Git repository
$ echo "print('Goodbye, World!')" > goodbye.py # create a new Python file
$ git add goodbye.py # add the file to the repository
$ git commit -m "Goodbye commit" # commit the changes
Keep practicing these steps until you're comfortable with the process. Happy coding!