DEV Community

vinayak
vinayak

Posted on • Originally published at itsvinayak.hashnode.dev on

GIT For Beginners

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.

Image description

Basic Git Commands

C reating commands

  • Initializing a Repository :
git init

Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

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)>

Enter fullscreen mode Exit fullscreen mode

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"

Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

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

  • Switching Branches :
git checkout <branch_name>

Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

  • Pushing Changes to Remote Repository:
git push

Enter fullscreen mode Exit fullscreen mode

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

Viewing commands

  • Checking Repository Status :
git status

Enter fullscreen mode Exit fullscreen mode

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

  • Viewing Commit History :
git log

Enter fullscreen mode Exit fullscreen mode

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

Top comments (4)

Collapse
 
bcouetil profile image
Benoit COUETIL ๐Ÿ’ซ

What is bashCopy ?

Collapse
 
itsvinayak profile image
vinayak

Notion to dev

Collapse
 
bcouetil profile image
Benoit COUETIL ๐Ÿ’ซ • Edited

Sorry I don't understand what you tried to answer, but since you removed the line, do not bother, it's fine ๐Ÿ˜…

Collapse
 
bcouetil profile image
Benoit COUETIL ๐Ÿ’ซ

What about git rebase, far more used than git merge on Github and Gitlab (that you tagged) ?