Git for Beginners: Basics and Essential Commands

BSc CSIT Graduate
Introduction: What is git?
Git is a version control system. It helps developers to track changes in files. It tracks all the information like: who changes the code, what changes are done, when the changes are done. It also lets you revert any changes if needed. Git uses diffing algorithm to track changes. Git enables developers to work on same project independently without breaking others code. Git allows every user to have the local copy of the repository(so that we call it distriubuted)
we use platform like github, gitlab,bitbucket, aws code commit, azure, git tea etc to store our local repository on the internet and enable collaboration with other developers.

Why git is used?
Git is used because it solves the real problems of developers:
Keep track of every change in code(when it is made, who made it)
Provide better collaborative environment
Prevents code loss
Helps to roll back if you realised the change was a mistake.
How to install git?
To get started with git, we need to install git in our machine first
For windows:
visit the officail git website: https://git-scm.com/install/windows
Download the latest installer (64-bits is recommended) and run it.
For MacOs: install home brew if not already and then run following command
brew install git
After installing git successfully, you need to set your email address and username. This is important because git commit willl use this information and permanently save in the commits you create.
git config --global user.name "Your Name"git config --global user.email "youremail@example.com"
In this , —global means your information will be used by all the repository in your machine.
Make sure by running command below:
git config --list
this will show something like this:
user.name="Your Name" user.email="your email address”
Now every commit you create will contain your your name and email address as author.
Git core terminologies
Repository
Any project maintained by git is called repository. A repository is the main project folder that git tracks. Every changes in the files inside this folder. This folder contains project files, git history and the branch information. Once we run
git initcommand, git starts tracking changes on our files and the folder becomes repository.Commit
Commit is the snapshot of your code at a point of time. It has a unique id(hash) and message. Or we can say any version of your project is a commit. If you permanenlty save your changes to your project with git, it is a commit. You basically move changes in your staging area to the repository area, that’s a commit. To move you use
git commit -m “message to explain the change”.Branch
When many developer works on the same project or even when you are working on new feature by yourself, you do not want to break your code, then you use soemthing called branch. Branch is a independent and separate line of development in your repository. It allows developers to work on new feature , bug fix etc without affecting the main version or stable version. Once your work is done you have to merge it into the default branch i.e. master or main.
Head
Head is a pointer that points to the current branch you are working on and the latest commit on that branch. When you make new commit, head updates HEAD to point to the new commit. You can move HEAD to a different branch or commit using
git checkoutorgit switch.Remote
Remote refers to the online-hosted version of your repository. It allows many developers to collab and share changes. Default remote name is usually ‘origin’.
You can run:
git remote -vshows the remote repository linked to your local repository.git remote add <any_name_(origin is default)> <url>to add new remote repository to your local repositorySimilary you can remove and rename too.
After adding remote repo to your local repository , you push your commits over there.
git push -u origin main-usetsoriginas the default so futuregit pushcommands can be run without specifying it.(-usets the default remote and branch for your local branch, so you don’t have to type the remote and branch every time you push or pull.)Clone
Clone is used to copy the remote repository to your local machine.
git clone <repo_url>This creates local repository with all the commits, files, and branches for the remote repository and allow you to work on the project locally.
Merge
Merge is the process of combining changes from one branch into another, usually merging a feature or bug-fix branch into the main branch. Git automatically integrates the changes, but if the same lines were edited in both branches, you may need to resolve conflicts manually. After merging, the target branch contains all commits from both branches, preserving their history.
git checkout main// swich to main branch(where you wanna merge other branch)git merge <brancName>// this will mergePush
Push refers to sending commits you did on the local repository to the remote repository so that other can see and access the change.
Pull
Let's say some collaborator to your project added some code on the remote repository and you want sync your local repository with the new changes, for doing this we need to download the repository changes. We can do it using the
git pull origin maincommand.
Three areas in Git
To maintains our files git uses three areas:

1. Working Area
Wokring area or working directory is the actual folder that we are writing code on. This is where we write code, make changes or delte files. Once we run git init command on this folder, git starts tracking changes on our files. Git just notices the changes does not save them in its history yet.
2. Staging Area
Working area is where we write, edit code. Once git notices these all changes if we decided to save the change, git put those changes in the staging area. Git does not do it automatically we have to run git add <filename> or if you want all the changes git add . . Any changes if file/files present in this area are those which wil be part of next version/commit*(in the world of git version is also referred as commit).*
2. Repository Area
Repository area or local repository where we hve those changes which are already part of some earlier vesion. When the files in the staging area are committed using the git commit command, Git stores those changes in the repository along with a commit message, date, and unique commit ID.
Read about git command here




