Git is a Distributed Version Control System (DVCS) that tracks every modification made to a project's files over time. It allows developers to create a complete historical record of their codebase, enabling them to revert to any previous, stable version instantly. Unlike older centralized systems, Git ensures every user has a full, local copy of the entire project history, making it robust, fast, and capable of functioning completely offline.
Git is essential for professional development because it enables safe collaboration. It allows teams to work simultaneously on different features using isolated branches without interfering with the stable code. Furthermore, Git acts as a safety net, allowing developers to easily fix bugs, undo mistakes, and maintain an organized history through meaningful commit messages.
First, create a folder and open it in the Git Bash terminal.
Then enter your name and email id (required for first-time users) using the following commands:
git config --global user.name "Your name"git config --global user.email "Your email"
After doing this, you have to initialise a git repository locally:
Command: git init
Now create files using: touch filename.extension
Once you initialize a repository and add files, check the status using: git status.
It will initially show Untracked files (files Git is not watching yet).
To track a file, use: git add "file name".
When you are modifying a file, it is in the Working Tree.
When you are done, you move it to the Staging Area using: git add "file name".
Commit means taking a snapshot of your staged files.
Command: git commit -m "descriptive message".
After committing, files become Unmodified. If you edit them again, they return to the Working Tree as Modified.
The following diagram describes the workflow cycle:
.gitignore is a file containing names of files/folders Git should ignore.
Create it using: touch .gitignore.
filename.txt/filename.txtfolder_name/*.extension (e.g., *.css)Why? To work on new features without breaking the main code (Production).
Commands:
git branch "branch_name"git branchgit checkout "branch_name"git checkout -b "branch_name"git merge "branch_name"The version of your git project which resides locally on your computer (your hard drive). This is where you work, edit, and commit changes before sharing them.
The version of your git project which is hosted on the internet or a hosting platform (like GitHub, GitLab). This is used for backup and team collaboration.
Used to switch branches or undo changes in a file before they are staged.
Undo changes in one file: git checkout "file name"
Undo changes in ALL files: git checkout -f
Used to check your commit history.
View all commits: git log
View last 'n' commits with details: git log -p -n
Used to see exactly what lines of code changed.
git diffgit diff --stagedUsed to remove files.
git rm "file name"git rm --cached "file name"This process links the project on your laptop (Local) to a project on the internet (Remote).
Go to your GitHub profile and create a new repository.
Important: If you already have code on your laptop, do not check the "Add a README file" box on GitHub. This avoids conflict errors.
Copy the HTTPS URL from GitHub (it ends with .git). In your local terminal, type:
git remote add origin "https://github.com/YOUR_USERNAME/REPO_NAME.git"
Explanation: "origin" is just a standardized nickname for that long URL. It tells Git where to send your files.
By default, local Git might name your branch "master", but GitHub uses "main". It is best practice to make them match.
git branch -M main
Now send your commits to GitHub for the first time:
git push -u origin main
Explanation: The -u flag sets the "upstream". This remembers the connection so in the future, you can just type git push.
Cloning is used when you want to download a copy of a repository from GitHub to your computer. This is useful when you want to contribute to an open-source project or download a project you found online.
What actually happens: Git creates a new folder on your computer, downloads all the files, and crucially, downloads the entire commit history (every version of the project ever made).
Command: git clone "https://github.com/username/repo_name.git"
Note: You do not need to use git init before cloning. The clone command initializes the repository for you automatically.
If you are working in a team, other developers might push code to GitHub while you are sleeping. If you try to push your code, Git will reject it because your local version is out of date.
To fix this, you must "Pull" the latest changes.
Command: git pull origin main
Behind the scenes: A git pull is actually two commands combined:
git fetch (Downloads the new data from GitHub but doesn't touch your files yet)git merge (Combines that downloaded data into your actual working files)Since 2021, GitHub no longer accepts account passwords for Git operations in the terminal for security reasons. If you try to push and type your regular password, it will fail. You must use one of the following methods:
A PAT is like a long, random password generated by GitHub that you use instead of your real password.
How to do it: Go to GitHub Settings -> Developer Settings -> Personal Access Tokens -> Generate New Token.
When the terminal asks for a password, paste this token.
SSH (Secure Shell) keys are the industry standard for authentication. It allows your computer to talk to GitHub securely without you typing a password every time you push.
The Concept: Think of it like a specialized "Lock and Key".
When you push code, GitHub checks if your Private Key matches the Public Key they have. If it matches, you are allowed to push.
How to set it up:
ssh-keygen -t ed25519 -C "your_email@example.com"cat ~/.ssh/id_ed25519.pubssh -T git@github.com