USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Git Fundamentals and Repository Setup

Lesson 9/32 | Study Time: 20 Min

Git is a distributed version control system widely used for tracking changes in source code during software development. It enables multiple developers to collaborate on projects efficiently by managing code revisions, branches, and history.

Understanding Git fundamentals and how to set up a repository are essential skills for developers, as they provide a structured workflow for managing changes, maintaining code integrity, and facilitating teamwork. 

What is Git?

Git tracks changes to files by taking snapshots of their entire state, called commits. Each commit has a unique identifier (SHA) and metadata like author and timestamp.

Git stores data as a series of commits organized in branches representing different development lines. It supports distributed collaboration—developers clone full repositories, work locally, and push changes to shared remotes.

Setting Up Git: Installation and Configuration

Before starting, Git must be installed on your system. Check installation with:

text
git --version


Configure your identity globally:

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

This information is used in commit metadata.

Creating a Git Repository

Two types of repositories exist:


To create a new local repository in an existing project directory:

text
cd path/to/project
git init


This command creates a .git directory, turning the folder into a Git repository ready to track changes.


To clone an existing remote repository:

text
git clone https://github.com/user/repo.git

Staging and Committing Changes

Changes are first staged in an index before committing to the repository.


  • Stage files:
text
git add filename


Or add all changes:

text
git add .


  • Commit staged changes with a message:
text
git commit -m "Commit message describing changes"

Each commit represents a stable snapshot of the project.

Checking Repository Status and History

git status shows the state of the workspace and staging area including untracked, modified, and staged files.

git log displays commit history with details.

git diff shows differences between working files and previous commits.

Remote Repository Setup and Pushing Changes

To work collaboratively, link local repositories with remote repositories:


  • Add a remote:
text
git remote add origin https://github.com/user/repo.git


  • Push commits to remote:
text
git push origin main

Replace main with your default branch if different.