Git : Basic And Essential Commands
What is Git
Git is a version control system .
in easy words :- Git tracks the changes in our code (file) so we don’t lose the code and go back to older version anytime.
on Git many programmers can work together without overwriting each other works.
Why Git is used
git helps programmers save , track , and manage code safely.
To save your work
What you changed
when you changed
why you changed
so even if you delete something by mistake —> Git can restore it
To work without fear
Experiment with code without fear
Even try new ideas and concepts
To work with a team
Many people can work on same project.
For example:-
One person design the website(htma & css)
otherone do the backend (how the icons or buttons work ).
To track progress
Git shows :-
Who changed the code.
What was changed.
When it changed.
very useful in real projects.
Git basics and core terminologies
core git concepts
These are the heart of git.(repository , commit , branch , head )
Repository(Repo)
repository is folder where:-
Your project file live.
Git tracks all changes.
History is stored.
Types:-
Local repository —> On your computer
Remote repository —> On git server
example :- School bag
books=files
notes history (like date section) :- git tracking.
Commit
A commit is a saved snapshot of your project .
“According to git :- I will remember your project exactly like this”
Each commit has :-
Unique ID (hash)
Message (what you changed)
Branch
A branch is a separate line of development.
Try new features.
fix bugs
experiment safely
Example :- Roads from a highway
Main road = main branch
side road = side branch
join back later = merge
Head
Head is like a pointer that tells “ Where are you right now in git”.
head points to The latest commit of the current branch.
Example :- pointer on a map which shows current location.
Simple diagram
commit A —> commit B —> commit C <—-Head
head is on commit c.
| Git term | Real life |
| Repository | Notebook |
| Commit | Page saved |
| Branch | Extra rough notebook |
| Head | Bookmark |
Cmman git command
git init , git status , git add , git commit , git log, git branch , git switch ,git merge , git push , git pull
git init
Turns normal folder to git repository.
When to use:- Starting a new project.
git status
shows the current status of project.
It tells
Which file changed
Which are staged
which are not tracked
git add
save the changes in files
git commit
saved a snapshot of changes
git commit -m
git log
shows commit history
you see :-
Commit ID
Author
Date
Mesaage
git branch
shows , creates or deletes branches
show branches :- git branch
create new branches:- git branch feature- login
git switch
moves to another branch
ex:- git switch feature- login
Head moves to that branch
git merge
combine one branch to another
ex:- git merge feature- login
git remote
connect local repo to git server
ex:- git remote add original URL
git push
sends commits to git server.
ex:- git push main server
git pull
downloads latest changes from git server
ex:- git pull main server
git clone
copies existing repository from git server.
ex:- git clone URL
Summary
| Command | meaning |
| git init | start git |
| git status | check changes |
| git add | stage changes |
| git commit | save snapshot |
| git branch | manage branches |
| git switch | change branches |
| git merge | combine branches |
| git push | upload |
| git pull | Download |
| git clone | Copy repo |