GIT For Beginners

GIT For Beginners

Version Control System

ยท

4 min read

Git is a distributed version control system (VCS) that allows developers to track changes in their code, collaborate with others, and manage software development projects efficiently. Developed by Linus Torvalds in 2005, Git has become one of the most popular Version Control System VCS tools in the software development community due to its flexibility, speed, and powerful branching capabilities.

Before diving into the details of how to use Git, it's essential to understand some fundamental concepts:

  • Repository (Repo): A repository is a directory where your project and its version history are stored. It contains all the files and directories relevant to your project, along with the history of changes.

  • Commit: A commit is a snapshot of the changes made to your repository's files at a specific time. Each commit has a unique identifier (SHA-1 hash) and includes information such as the author, date, and commit message.

  • Branch: A branch is a separate line of development in a repository. It allows you to work on new features or bug fixes independently from the main codebase. Branches enable parallel development and prevent conflicts between different features.

  • Master/Main Branch: The main branch in a Git repository is usually called "master" or "main." It represents the stable version of your project and should ideally contain only production-ready code.

  • Remote: A remote is a copy of a repository stored on a different server or location. It enables collaboration between multiple developers by allowing them to push and pull changes from a shared codebase.

  • Clone: Cloning creates a local copy of a remote repository on your machine. It allows you to work on the project locally and sync changes with the remote repository when necessary.

  • Pull: Pulling is fetching and integrating changes from a remote repository into your local branch.

  • Push: Pushing is the process of sending your local commits to a remote repository, and updating it with your latest changes.

  • Merge: Merging combines two or more branches' histories, incorporating the changes from one branch into another.

  • Pull Request (PR): A pull request is a request to merge changes from one branch (typically a feature branch) into another (often the main branch). It is commonly used in collaborative development to review and discuss code changes before merging.

Basic Git Commands

Creating commands

  • Initializing a Repository:
git init

This command initializes a new Git repository in the current directory, creating a hidden .git folder to store version control information.

  • Cloning a Repository:
git clone <repository_url>

This command clones an existing remote repository (github/gitlab) to your local machine, creating a new directory with the repository's contents.

Tracking files

  • Adding Changes to the Staging Area:
git add <file(s)>

This command stages specific files or all changes in the working directory, preparing them for the next commit.

  • Committing Changes:
git commit -m "Your commit message here"

This command creates a new commit with the changes that have been staged in the previous step. The -m flag allows you to add a commit message to describe the changes.

Branching

  • Creating a New Branch:
git checkout -b <branch_name>

This command creates a new branch with the specified name and switches to that branch.

  • Switching Branches:
git checkout <branch_name>

This command switches to the specified branch, allowing you to work on that branch.

  • Merging Branches:
git checkout <target_branch>
git merge <source_branch>

These commands merge the changes from the source branch into the target branch. The checkout command switches to the target branch and the merge command incorporates changes from the source branch into it.

Updating commands

  • Pulling Changes from Remote Repository:
git pull

This command fetches and merges change from the remote repository into the currently checked-out branch.

  • Pushing Changes to Remote Repository:
git push

This command sends the committed changes from your local repository to the remote repository.

Viewing commands

  • Checking Repository Status:
git status

This command displays the current status of the repository, showing which files have been modified, staged, or are untracked.

  • Viewing Commit History:
git log

This command displays a log of all commits in reverse chronological order, showing commit information like author, date, and commit message.

Did you find this article valuable?

Support Vinayak Sharma by becoming a sponsor. Any amount is appreciated!

ย