Creating your First Git Repository

Git is a distributed version control system that allows you to track changes to your files and collaborate with other developers. GitHub, in turn, is a code hosting platform that uses Git to manage collaborative development projects. In this chapter, we'll cover the process of creating your first Git repository and how to host it on GitHub.

Installing Git

Before creating a repository, you need to have Git installed on your system. Installation varies depending on the operating system:

  • Windows: Download the Git installer from git-scm.com and follow the instructions installation instructions.
  • macOS: You can install Git using Homebrew with the command brew install git in the terminal, or download the installer from the official website.
  • Linux: Use your distribution's package manager, for example, sudo apt-get install git for Debian-based systems.

Configuring Git

After installation, it is important to configure your Git identity. Open the terminal and type the following commands:

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

Creating a Local Repository

To start a new Git repository, you first need to create a folder for your project if you don't already have one. Then, navigate to that folder in the terminal and run the command:

git init

This will create a new subfolder called .git that contains all the files needed for versioning your project. You now have an empty Git repository ready to go.

Adding Files to the Repository

With the repository created, you can start adding files. Create or copy the files to the project folder and use the following commands to add files to version control:

git add file_name (to add a specific file)
git add . (to add all files and changes to the current folder)

Committing Changes

After adding the files, you need to "commit" the changes, that is, save a point in your project's history with a message describing what was done. Use the command:

git commit -m "Message describing the change"

Creating a Repository on GitHub

To host your Git repository on GitHub, you need to create an account on github.com if you don't already have one. After logging in, follow these steps:

  1. Click the "+" icon in the top right corner and select "New repository".
  2. Fill in the name of your repository, an optional description, and choose whether it will be public or private.
  3. Click on "Create repository".

Connecting the Local Repository to GitHub

After creating the repository on GitHub, you need to connect your local repository to the remote repository. In the terminal, inside your project folder, run the command:

git remote add origin URL_DO_REPOSITORIO

Replace "URL_DO_REPOSITORIO" with the URL of your GitHub repository, which you can copy from the GitHub repository page.

Pushing Changes to GitHub

To push your local changes to GitHub, you will use the "push" command. If this is your first push to the repository, you need to set the default branch:

git push -u origin main

Replace "main" with the name of the branch you want to push, if different. After the first push, you can simply use git push to push your changes.

Cloning an Existing Repository

If you want to start working on a project that is already on GitHub, you can clone the repository using:

git clone URL_DO_REPOSITORIO

This will create a local copy of the repository on your system.

Conclusion

Creating your first Git repository is a fundamental step in managing software projects. With Git, you can keep a detailed history of changes, try new ideas in separate branches, and easily collaborate with other developers. GitHub offers a robust platform to host your Git repositories and share your work with the world. By following the steps outlined in this chapter, you will be well equipped to start using Git and GitHub in your development projects.

Now answer the exercise about the content:

Which command is used to start a new local Git repository after creating a folder for the project?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image The lifecycle of files in a Git project

Next page of the Free Ebook:

9The lifecycle of files in a Git project

4 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text