What is git? how use it ?
What is version control system ?
we play games, when we reached a particular checkpoint, we want to save because if we fail to complete the level the game restart from the last checkpoint instead of the beginning. The same concept is version control system.
version control system continuous tracking and managing out our software code. By version control system big teams can manage the source code.
If any mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake.
Git is a version control system.
What is git?
Git is a version control system. Git was developed by Linus Torvalds in 2005 for the development of the Linux kernel.
Install & setup git:
Frist we install git, install git from here,
After install, if we want to cheek the version of git,
git --version
Then we configure git,
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
if want to see git configure settings (which is same in all repository of our local device) we use,
git config --list
By default git not track any file or folder in our computer, if we want that git start tracking our files, we should create a repository.
What is repository?
Repository is fancy name, the simple meaning is a folder which is tracked by git is called repository.
How we cheek that our folder is tracked by git or not ?
by simple command,
git status
if we want to convert a simple folder to a repository, we use:
git init
after this command in our folder, a new folder created which name is .git (hidden by default).
The Flow of git:
At the first time every file & folder are untracked, we have to tell git to track them. So we have to stage them or add them, so we use:
git add <file> <file2>
if we want to add all file and folder in our repo then we use:
git add .
then we write code, and after write code if we cheek the current scenario of our repo we see some files are modified, if we write a particular code and we want add a checkpoint over there we use commit, it’s like a snapshot of a particular time. After every commit we can always come back previous commit.
git commit -m "commit message"
then we write code again, then add them , then commit, this the flow of git.

if we want to see all commits we are made, we use git log command,
git log
there exist a commit id with every commits are made. but the git log is little messy so we use git log —oneline
git log --oneline
time travel:
we want to go back previous commits, there are two way to that,
first way, we want delete every commit,
soft way: We want to keep our current code and combine all commits after a specific previous commit into one, so we use git reset --soft
git reset --soft <commit-hash>
hard way: If we don’t want any code changes after a specific previous commit, we use git reset --hard.
git reset --hard <commit-hash>
Some Behind the scenes:
Git commit is like a snapshot which representation of the code at a specific point in time. Git stores information about the code in a locally stored key-value based database. Everything is stored as an object and each object is identified by a unique hash code
3 musketeers of git:
commit object
tree object
blob object
Branches in Git:
Branches is like different timelines of code. In real life development there exist one main branch or production branch, when we need to create a new feature or fix some bug we create a separate branch and doing work and when our work is done we merge the branch with the main branch.
By this way many developers can collaborate easily and doing their work, some developers can work on Header, some can work on Footer, some can work on Content, and some can work on Layout.
what is head?
In git head is pointing to the commit of the current branch we are working on.
the default branch used to be master, but it is now called main.
the flow of branches:
to check how many branch in our repo and in which branch we are working on, we use;
git branch
to create a new branch we use,
git branch <new branch name>
to switch to the specific branch we use,
git switch <new branch name>
after switch, if we run git log it shows only that branch’s commits.
git switch -c dark-mode - This command creates a new branch called dark-mode. the -c flag is used to create a new branch, we also switch to that branch,

we create a new branch then add new feature or fix bug or something, now we combine it with the main branch, here the merging comes.
Merge Branches:
there are two types of merge,
Fast-Forward Merge
3 way Merge
Fast Forward merge is easy scenario, we create a branch from main branch then we create something then we want to merge it with main branch, so we first switch to the branch where we should merge, then we use,
git merge <branch name>
where we merge, there creates a merge commit. but here a problem, what branch we merge to the main the commits of that branch also comes in main branch. We don’t want that because it destroy our main branch beautiful commit history with unnecessary commits. so we merge with squash,
git merge --squash <branch name>
it combine all commits of that branch to a single merge commit.
3 way merge:
Imagine a problem scenario, we create a branch from main branch, then someone finish there work and merge to main branch, so now we behind one commit from main branch. We are working, during our work again someone create a new feature and merge to main branch. now we behind two commits, so after finish our work we merge to main here the conflicts comes.

In a fast-forward merge, there are no conflicts. But in a not fast-forward merge, there are conflicts, and there are no shortcuts to resolve them. You have to manually resolve the conflicts. Decide, what to keep and what to discard.
