Mastering Git: A Comprehensive Guide

Mastering Git: A Comprehensive Guide

What is Git:-

  • Git is a distributed version control system, which means that each developer has a complete copy of the entire codebase and its history

  • This makes it easy to work on projects from anywhere and share changes with others

Why Git:-

  • Developers can work together from anywhere in the world.

  • Developers can see the full history of the project.

  • Developers can revert to earlier versions of a project.

Benefits of Git:-

  • Offline working

  • Distributed development

  • Creates backups

  • Free and open-source

Tracks history

Git Workflow:-

Git has three main stages:

  1. Working Directory:

    • The working directory is the directory on your file system where you're currently working. It contains the files of your project.
  2. Staging Area:

    • The staging area is a middle ground between your working directory and the repository.
  3. Committed:

    • The committed state is where the changes are permanently stored in the Git repository

Installation of Git:

Git is available for various operating systems, including Windows, macOS, and Linux. You can download the latest version of Git from the official website: https://git-scm.com/downloads

Once you've downloaded and installed Git, you can open a terminal or command prompt and start using Git commands

Configuring Git:

Before using git you have to configure you username and email .

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

General Git Features:-

Initializing Git:-

To start using git in your project you have to initialize git into your project.

git init

This command creates a new Git repository in the current directory

Checking the Status of Your Repository:-

git status

This command shows you the files that have been modified, added, or deleted since your last commit.

Staging Changes:-

Before you can commit your changes, you need to stage them.You can stage files using the git add command.

git add file1.txt file2.js

This command stages the changes made to file1.txt and file2.js. You can also use the following command to stage all modified files:

Viewing the Commit History:-

You can view the commit history of your repository using the git log command

git log

This command displays a list of all the commits made to your repository, including the author, date, and commit message

Branching and Merging:-

Git's branching and merging capabilities are among its most powerful features. Branches allow you to work on different features or bug fixes in parallel without affecting the main codebase.
We can even switch between branches and work on different projects without them interfering with each other

Creating a New Branch:

To create a new branch, use the git branch command

git branch new-feature

This command creates a new branch called new-feature.

Switching to a Branch:-

To switch to a different branch, use the git checkout command

git checkout new-feature

This command switches to the new-feature branch.

Making Changes on a Branch:-

Once you've switched to a branch, you can make changes to your codebase as usual. When you're ready to merge your changes back into the main branch, you'll need to commit your changes and switch back to the main branch

Merging a Branch:

git checkout main

Then, use the git merge command to merge your branch:

git merge new-feature

This command merges the changes from the new-feature branch into the main branch.

Other Commands:-

Deleting a Branch:-

git branch -d <branch name>

This command will delete your branch.

Merging two Branches:-

It’s preferred to change/switch to the master branch before any branch needs to be merged with it.

git merge <branch name>

This will merge the specified branch with our master branch

Conclusion:-

Git is a powerful and essential tool for any developer working on software projects. By understanding its concepts, commands, and best practices.
This guide has covered the basics of Git, as well as more advanced concepts and best practices. Whether you're a beginner or an experienced developer, there's always more to learn about Git