Your Beginner’s Guide to Starting a Git Repository

Praveen Maurya
3 min readDec 28, 2023

--

Are you new to the world of coding and collaboration, wondering how developers manage and share their work effectively? Git, a powerful version control system, is the solution. This blog will walk you through the process of setting up your very first Git repository. We’ll explain each step clearly so even a complete beginner can understand. Plus, we’ll demystify the .gitignore file and its importance.

Starting Your First Git Repository

Step 1: Creating a Git Repository

Imagine Git as a smart system that keeps track of changes in your code. The first thing you do is set up your Git repository using the git init command:

git init

This simple command tells Git to begin tracking your project and its changes.

Step 2: Adding Files for Tracking

Before Git can keep an eye on your code, you need to tell it which files to watch. The git add command does just that. For instance, if you have a file called README.md, you add it to the list of files to track like this:

git add README.md

Think of this as telling Git, “Hey, watch this file; it’s important!”

Step 3: Saving Your Work

Now, let’s save your changes in Git. When you make changes to your code, Git can save snapshots of those changes. Use the git commit command to create a snapshot. It’s like taking a photo of your project at that moment. You also provide a short message to describe the changes:

git commit -m "First commit"

With this step, Git keeps a record of what your project looked like at different points in time.

Step 4: Naming the Main Branch

By default, Git names the primary branch “master,” but many now prefer “main” as a more inclusive term. You can change it with the git branch -M command:

git branch -M main

It’s like deciding what to call the main road in your project town.

Step 5: Connecting to a Remote Repository i.e. GitHub or GitLab via Command Line

If you want to work with others and share your code, you need a remote repository (often hosted on platforms like GitHub).

Create a New Repository on GitHub or GitLab

Begin by visiting the GitHub or GitLab website and logging into your account. Create a new repository following the platform’s guidelines.

Use the git remote add command to connect your local repository to a remote one. The “origin” is a common name for your remote repository, and the URL points to its location:

git remote add origin https://github.com/your-username/your-repository.git

This step is like telling your local library where to find a book — you specify the location (URL) of your project’s online copy.

Step 6: Sending Your Code to the Remote Repository

Finally, when your code is ready to share, use the git push command to send it to the remote repository. The “-u” flag is used to set up a connection between your local and remote repositories, so you can simply use git push in the future:

git push -u origin main

This step is akin to uploading a photo to a social media platform, making your code accessible to others.

Unveiling the .gitignore

Now that you’ve started your Git journey, let’s talk about the .gitignore file. This file is like a list of items you don’t want to include in your project history. It tells Git to ignore specific files or folders.

To create a .gitignore file, simply create a new text file in your project directory and name it “.gitignore.” In this file, you list the names of files or directories you want Git to ignore.

For example, your .gitignore might look like this:

# Ignore system files
.DS_Store

# Ignore compiled code
*.class
*.jar

# Ignore sensitive information (add your own)
secrets.txt

With .gitignore, you prevent irrelevant files and sensitive information from being included in your Git history, keeping your project clean and secure.

In conclusion, setting up your first Git repository is a beginner-friendly process. Git allows you to track changes in your code, collaborate with others, and maintain a clean code base using the .gitignore file. By following these steps, you’ll be on your way to becoming a confident Git user. Happy coding!

--

--

Praveen Maurya
Praveen Maurya

Written by Praveen Maurya

0 Followers

Life should be interesting.

No responses yet