How to get started with Git: A Beginner's Guide (Part 1)

4 min read

Cover Image for How to get started with Git: A Beginner's Guide (Part 1)

Introduction

Have you ever accidentally overwritten a crucial file or lost track of changes you made to your project? Or worse do you still maintain and keep copies of your project on your system? If you work with digital files, this can be a real nightmare. Version control systems (VCS) like Git come to the rescue, offering streamlined project management and collaboration. But for beginners, Git can seem intimidating. Worry not! This beginner's guide to Git will help you navigate the world of version control effortlessly.

How to get started with Git

What is Git?

Git, a free and open-source Version Control System (VCS), tracks changes in computer files and directories. It acts as a time machine for projects, enabling users to rewind to any point in time, access historical versions, and revert changes if necessary. This fosters smoother collaboration in projects and facilitates multiple people working on the same project simultaneously, minimizing conflicts.

Git vs Github: The most basic confusion!

Git is the tool you use on your local machine to manage versions. GitHub, on the other hand, is a web-based platform that allows you to store your Git repositories (collections of files and their version history) in the cloud. You can use Git without GitHub, but GitHub provides a great way to share your work, collaborate with others, and track your project's progress.

Git vs Github

How to get started with Git

Git Installation

The first step is to install Git on your computer. Head over to the official Git website and download the installer for your operating system. Once installed, you can verify that Git is working by running the following command on your terminal

git --version

Configuring Git

Once Git is installed you'll need to configure it with your name and email address. Run the following command to do so

git config --global user.name "Your Name"
git config -- global user.email "[email protected]"

These settings will be used for all your Git repositories unless you override them for a specific project.

Creating Your Repository

A repository or a repo as we call it, can be imagined as a treasure chest that stores all project's files and remembers all the changes you've ever made. To create a new repository, open your terminal and navigate to your project directory. Then type the following command:

git init

This simple command initializes a new Git repository in your current directory.

Track Your Changes

For tracking all your changes in the repo you use another simple command, which tells git which files you want to track. Use the git add command followed by the filename to tell Git to keep an eye on that file. Think of it as marking a document for grading. In Git words the command "stages" them for the next step.

For files you've modified but don't want to track, you can use .gitignore (a special file) to tell Git to exclude them. See here

git add myfile.txt     # Stage a specific file
git add .              # Stage all changes

Commit Your Changes

The next step is to create a permanent record of your changes in the repo. Which can be done by git commit. Regularly use git commit to capture a snapshot of the staged files along with a descriptive message about what changed.

git commit --message "My first commit"

Here --message can also be substituted with -m flag

Viewing log History

You can view the commit history of your Git repo to see a log of all the changes that have been made over time. Use the following command to display the commit history

git log

Pushing to the Remote (Optional):

In this first part, we're focusing on local Git usage. However, Git truly shines when collaborating with others. To push your commits to a remote repository like GitHub (which we'll cover in future parts), you'll need additional setup steps. But for now, congratulations! You've created your first Git repository and made your initial commit.

Stay tuned!

In the next part of this series, we'll delve deeper into working with Git, exploring commands to manage your commits, branches, and more. We'll also discuss best practices for effective Git usage.

References