Git for Beginners
What is Git?
Git is a version control system that helps you track changes in your code, save different versions of your project, and work with other developers without breaking anything. In simple works, it used to keep track of your changes in code and other files. It allows multiple developers to work on the same project at a time.
Git works like a time machine for you project. Each time a developer saves(commit) some code, Git create a snapshot of the project and allow to revert the files back in anything went wrong.
Why Git is used?
In order to understand the usage of Git, let’s first take a look this example.
A real life example without git
Imagine you are working on a project todo-app . You have created new features and on each feature completion you save it as a copy folder. Your folders will look like this:
todo-app
todo-app-final
todo-add-final-v2
todo-app-final-v3
todo-app-final-v4
todo-app-final-really-final
Problems:
You can’t keep a track of features created
You don’t really know which changed between folders/versions
If some issue happen, you cannot go to older version of code easily.
You might accidentally delete the working code.
If multiple developer working on the same, It will be nightmare to handle the folders.
How Git helps?
When you initialise git for some folder, Git keeps tracks of the changes internally.
It allows you to go back to any previous versions
See who and when the code or files has been changed
Undo or go back to previous version easily in seconds.
Git is like a gaming save system:
You save the game.
When you die → you restart from the saved games.
Git in code:
You saves(commit) the code
If some issue happen, you goes back the older versions.
Git Core Terminologies
Git key concepts includes key concepts of the version control. These are essential terms which helps developers to collaborate and manage codes or files in a project efficiently.
Repository: Central location for all your source code and complete history of changes. In simple words, the folder which contains all you files and git directory. It can locally (on your system) or on remote server(Github, GitLab, Bitbucket..).
Git Working Directory: This is the directory which keeps track of your changes by Git.
Branch: As the name suggests, branch is a diversion from the git working repository or the main repository which keeps the same code from where it has been diverse. It usually created to do some changes and merge to the main repository, so that the main repository won’t be affected while working on the new changes.
Staging Area: The temporary area where the changes are prepared before committing them. Only the staged changes are committed to the branch.
Remote: A reference to a version of the repository hosted on a server, typically identified by a URL. The default remote is usually named origin.
HEAD: A symbolic pointer to the current branch or commit you are working on.
Common Git Commands
Git configuration commands
git config --globaluser.name"[name]": Sets the username for all commits made by the current user.—-globalis used to set user name on global level.git config --globaluser.email"[email address]": Sets the email address for all commits made by the current user.git init: Initialize a new Git repository in the current directory.git clone [url]: Creates a local copy of a remote repository.git config —list: List and displays all the configrations.
Branching
git branch: Lists all local branches in the repository.git branch [branch-name]: Creates a new branch.git checkout [branch-name]: Switches to a specific branch.git checkout -b [new-branch-name]: Creates a new branch and immediately switches to it.git merge [branch-name]: Integrates changes from a specified branch into the current branch.
Committing and Staging changes
git status: Shows the status of changes as untracked, modified, or staged.git add [file]: Stages a change in a specific file to be included in the next commit.git add .: Stages all new and modified files in the current directory and its subdirectories.git commit -m "[message]": Records the staged snapshots permanently into the project history with a descriptive message.git diff: Shows changes in the working directory that are not yet staged for the next commit.git log: Displays a list of commit history on current branch.
Working with remote repository
git remote add origin [url]: Adds a remote repository to your local configuration.git push origin [branch-name]: Uploads local branch commits to the remote repository.git pull: Fetches changes from the remote repository and merges them into the current local branch.git fetch: Downloads objects and refs from another repository (without merging into your current branch).
Undoing Changes
git reset [file]: Un-stages a file while retaining the changes in your working directory.git checkout -- [file]: Discards changes to a specific file in the working directory.git revert [commit-id]: Creates a new commit that undoes the changes made by a specified commit.git stash: Temporarily saves changes that are not ready to be committed, allowing you to switch branches.
Git flow and branching

Above diagram shows the flow of git and how code moves step-by-step from your computer to the remote repository. This way multiple developers works on the same project(repository) simultaneously without any issues.
Branching is also an essential concept in git as each developer must recommend to work on a separate branch.
Let’s take a example of 3 developers working on a repository. A repository always have a main branch either denotes as main or master branch. A developer One created a new branch “Branch 1“ from the main branch. This means the branch 1 contains all the code from the main branch till C3 commit. After completing his changes, the “Branch 1” gets merged into the “Main” branch. The same way developer Two creates another branch “Branch 2” and merged it back to the main. This way all the developer One and Two works on their individual task.

Each box represents a commit
Each commit stores a snapshot version of code
Each commit is linked with another commit represented as a line.
Conclusion
Git is a tool that helps you save your code history, undo mistakes, and work with others safely.
What Does Git Actually Store? Git stores:
Every file version
Every change
Every commit
Who changed it
When it was changed
But smartly and efficiently.




