Git : Basics and Essential Commands
What is Git?
Git is a Distributed Version Control System (DVCS). It keeps the track of Changes made in our project over the time.
It allows multiple developers to work together (or collaborate) on a single project.
It was created by Linus Torvalds in 2005.

(IMG : Git Workflow)
What does it mean to be Distributed VCS?
If a team of developers is working together (or collaborating) on a project. Then distributed means that each developer has its own copy of project along with the project’s version history. And they are able to work offline no need of internet.
Why should we use git?
Git takes snapshots at each time of commit. Instead a list of changes (deltas) made in a file.
It is distributed system. So able to work offline even in collaboration.
Git allows us branching. We can experiment with our project keeping the main code clean.
Git is very fast as all commands except the push/pull executes locally on our machine.
Git is free, open source and have a large community.
Git Basic and Core Terminologies :
Repository (or Repo) :
A storage space that contains the version history of your project. It is the database where Git permanently stores the snapshots (commits) of your project. It is the
.gitfolder. By default it is hidden folder.
(IMG Source : https://humbletoolsmith.com/2022/01/30/a-look-inside-the-_git-folder/)
Working Directory :
The folder which contains the actual files of your project and able to edit these. Git refers to this as “Working tree“.
Staging Area (or Index) :
A middle buffer between your working directory and database (Repository). It is a file which stores information about what will be saved next by commit. You pick file from the working directory and put them here.
Commit :
A permanent snapshot of your project’s state at a specific time. each commit has a unique id, hash id(SHA).
Head :
A pointer which points to the current commit or branch on which you are.
Branch :
An independent line of development. Usually project start with the branch named main or master.
Common Git Commands :
git init:This command initialize or create a new Git repository in your project directory. It convert your normal project directory into Git tracked project directory.
It creates a
.githidden folder in your directory. which will have entire history of your project.git status:It tell us about the current state of our working directory and staging area.
It mainly shows three things :
1. Untracked files : Newly created files which has never been seen before by git.
2. Changes not staged for commit : Files which are modified but haven’t moved to staging area for commit.
3. Changes staged but not committed : Files which are staged or in staging area, ready for the next snapshot or to be committed.
git add <filename>(The Staging Area) :This command is used to move those files which are changed or modified into the staging area.
It tells commit which file’s changes should be saved next.
git add <filename>: Stages a specific file.git add .: stages all changed and new files in the current directory.git commit(The Snapshot) :This command saves everything currently in the staging area permanently in repository.
we can use
-m “message“flag which allows us to attach a message about the commit we are committing.Internally Git creates a Commit Object. which contains, The Author, The Data, The Message, A Hash ID of commit, A Pointer to the previous or parent commit (This links the history together).

(IMG : Commit History)
git log(The History) :List the history of commits for the current branch. It allows us to check who changed what and when.
It includes the hash id of commit, the Author, the Date, and the Message.
git log: it gives detailed standard view.git log —oneline: it is the shortened view which shows the short hash and the message.git diff:It is used to check difference between any two commits what have changed. It uses
+ greenfor added lines and- redfor removed lines.git diff: compares the working directory and staging area.git diff —staged: compares staging area with last commit.git revert(The safe undo) :It undo a commit by creating a new commit which is negative of the previous commit. It does not remove the commit which has mistake or bug from the history/log.
git reset:It undo commit by moving the HEAD pointer backward to a specific commit.
git reset <hash>(Normal) : It moves back the head pointer. It unstages all the files. keeps the changes in working directory.git reset —hard <hash>: Moves back the head pointer and destroys the staging area and all the previous commit’s changes.git reset —soft <hash>: Moves back the head pointer. But keep changes as it is in staging area.