DEV Community

Cover image for Git for Beginners: Basics and Essential Commands
Vicky Gupta
Vicky Gupta

Posted on

Git for Beginners: Basics and Essential Commands

What is Git

Git is a software which is a Version Control System, which basically keeps the track of history changes, and helps in managing different timelines for developers

Why Git is Used

It is just used to keep the hustle out, when working in teams there might be conflicts while adding some feature or fixing a bug in the code.
Git is easy to learn, and adds a lot of value

Git Basics and Core Terminologies

repository
Enter fullscreen mode Exit fullscreen mode

A repository is basically like a container, with all the code files, folder in it. We can say it is like parent folder for all the code files

Commit
Enter fullscreen mode Exit fullscreen mode

It is like a snapshot of your whole at that point of time. Let's say i added a feature by adding 3 lines of code, then a commit will have information like: Author, Date & Time, Parent, Hash, etc.

Branch
Enter fullscreen mode Exit fullscreen mode

Imagine all the commits are set on a track, then this track of all the history of commits is called a Branch.

Head
Enter fullscreen mode Exit fullscreen mode

It is kind of a pointer which is automatically set to the latest commit.

Common Git Commands

There are hundreds of git commands, but generally you will use only a few of them in your lifetime. Some of them are mentioned below:

git init : This command sets up the folder/directory for your code. (A hidden .git folder is created in which all essentials are contained)

Local repository structure overview

git status : This tells you which files are modified, untracked, tracked

git add <file_name> : This pushes the targeted file to staging area(It means that these changes are ready to commit)

git add . : This adds all the files to staging area at once.

git commit -m "your_message" : This command is to save a change permanently. The -m flag is used to give the commit a message for example: if i added a function to multiply two integers, then the message can be: add a function for multiplication.

Commit history flow
The master is now called Main (it is the main branch)

git log : This Command shows the history of all the commits with all the information about each commit. For a compact output you can use git log -oneline

git branch : This shows all the branches for your repo.

git checkout -b "branch_name" : Creates a new branch and also switches to it at the same time.

git reset --hard <commit_hash> : Shifts the head to your desired commit and deletes the commits after that one.

git reset <commit_hash> : shifts the head, but the commits after that are transferred to staging area.

There are some of the most used and basic commands to work in git. If you want to read all the git commands read them here.

Basic Git workflow

Git working directory → staging area → repository

Conclusion

Git is a very helpful tracking system for your code, you can install it in your system from here, or you can simply use a remote like Github

Top comments (0)