Git for Beginners: Basics and Essential Commands

Introduction
Git is a widely used version control system. Git remembers what changed, when it changed, and who changed it.
It was created in 2005 by Linus Torvalds, the creator of the Linux kernel.
Lets understand what is git and why is it necessary in modern development
What is git?
Git is a tool that helps us track changes in our code, collaborate with others, and safely experiment without fear of breaking things.
Why Git is Used
Git is used to solve pendrive problem. Instead of storing our code in zips or pendrive we can use git and push our code to remote server. That remote server is treated as single source of truth. Each developer works on the codebase and pushes the changes to remote server so other developers can pull the changes.
This makes easier for developers to collaborate in a project and merging the changes of different developers becomes easy as well.
Git Basics
For using git we just have to initialize a repository. In the root of our project we just use git init command , it initializes the repository and starts tracking changes in our code. Git detects the changes but does not save them automatically.
Git uses two-step commit process , first we have to add the changes to staging area using git add <filename> command and then commit it using git commit -m “message“ command. Now our changes are saved.
Core Terminologies
Repository: A repositpry is a folder where Git tracks your project.
Staging Area: A temporary area where selected changes are prepared for commit
commit: A snapshot of staged changes saved permanently in the repository.
Remote: A version of your repository stored online.
Branch: An independent line of development used to work on features or fixes.
Head: A pointer that indicates your current branch and latest commit.
Common Git Commands
git init - Used to initialize a git repository.
git status - Shows the current status of files (modified, staged, untracked).
git log - Displays the commit history of the repository.
git add <filename> - Add a file to staging area.
git add . - Add all files in to staging area.
git commit -m ‘‘message’’ - Saves staged changes as a new commit with a message.
git diff - Shows changes between files, commits, or staging states.
git remote -v - Shows remote repository URLs linked to the project.
git remote add origin <url> - Connects a local repository to a remote server.
git push origin <branch> - Uploads local commits to remote server.
git pull origin <branch> - Pulls latest code from remote server.
Conclusion
Git is a tool that every developer must learn as it makes collaborating and maintaining the code much easier.
