Git is a powerful version control system that helps developers manage and track changes to their codebase over time. It enables collaboration, allowing multiple developers to work on the same project without overwriting each other’s work. Git tracks every modification, making it easy to roll back, track bugs, and collaborate efficiently.

Here, we’ll go through some essential Git commands and setup instructions that will help you get started with Git and GitHub.
Prerequisites

Before you begin using Git, you’ll need to have it installed on your system. Follow the instructions below based on your operating system:

Install Git On Windows
Install Git On Mac
Install Git On Linux
  1. Checking Git Version

Before starting with any Git operations, it’s essential to check if Git is installed correctly on your system. The following command will show you the version of Git installed:

git --version

  1. Initializing a Git Repository (git init)

To start tracking a project using Git, you need to initialize a new Git repository in your project directory. This can be done with the git init command.

Navigating to your Project Directory: Before using Git, navigate to your project directory:

cd command

Initialize Git Repository: Once you’re in the project directory, run the following command to initialize a Git repository:

git init

  1. Git Configuration

After that, configure your username and email:

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

  1. Forking and Cloning a Repository

Forking is a repository means creating a copy of an existing repository in your GitHub account so that we can make changes without affecting the original repository.

How to Fork a Repository

Go to the repository we want to contribute to on GitHub.
Click the Fork button at the top right.
Once forked, navigate to your GitHub profile and open the forked repository

Copy URL: Then a copy of real repository will be created in your local repository. After that, we have to copy the URL from your local repo. For doing that click to code and copy the URL.
  1. Cloning the Repository Locally

After forking, clone the repository to your local machine

Create a folder on your desktop where you want to store the project files.
Open Git Bash and navigate to the newly created folder using the cd command:

cd

Copy the repository URL from GitHub.
In Git Bash, type the following command and press Enter

git clone

The repository will be cloned into your desktop folder, making the project files available on your system.
  1. Checking the Status

After making code changes, check which files are not added using:

git status

This command displays the current state of your working directory, indicating whether files are untracked, staged, or committed. Files in red are untracked or modified but not staged, while files in green are staged and ready to be committed.
  1. Adding Files to Staging Area

When we get to know which files are not added by typing git status(red-colored files are not added).

To track a file or prepare changes for commit:

git add

To add all changes:

git add .

  1. Committing Changes

    To save your changes in the local repository, first check the status using:

git status

Files displayed in green are staged but not yet committed. To commit these changes, use:

git commit -m "Your commit message"

  1. Pushing Changes to GitHub

To upload commits to your forked repository

git push origin