Git for Beginners: Basics and Essential Commands

Search for a command to run...

No comments yet. Be the first to comment.
You open WhatsApp, type a message, and hit send. A single grey tick appears. You're on a train with no signal, but the message went through anyway. Or did it? That single tick is the beginning of a su

You record a Reel, add a song, trim it, and then close the app before posting. When you reopen Instagram, the draft is right there waiting. Nothing was lost. That experience feels simple. The system b

If you have built a React Native app, you have felt the pain of navigation setup. It is easy to get wrong and hard to keep clean as apps grow. This article explains what routing means, why it matters,

Modern apps are not just a pile of screens. They are a living system of features, data flows, offline behavior, and performance constraints. If you have only shipped small apps, the jump to something

Your application has user accounts. Some routes should only be accessible to logged-in users. How do you protect routes and verify that a request actually comes from who they claim to be? JWT (JSON We

Ashish's Blog
57 posts
Version control is an essential skill for any developer, and Git is the most widely used version control system in the world. Whether you're working on a personal project or collaborating with a team, understanding Git will make your development workflow smoother and more efficient. In this guide, we'll cover the fundamentals of Git and the essential commands you need to get started.
Git is a distributed version control system that helps developers track changes in their code over time. Created by Linus Torvalds in 2005, Git allows multiple developers to work on the same project simultaneously without overwriting each other's changes.
Unlike centralized version control systems, Git gives every developer a complete copy of the project history on their local machine. This means you can work offline, commit changes, and view the entire history of your project without needing constant access to a central server.
Think of Git as a time machine for your code. It takes snapshots of your project at different points in time, allowing you to review changes, revert to previous versions, or experiment with new features without fear of losing your work.
Git has become the industry standard for version control, and for good reason. Here are the key benefits that make Git indispensable for modern software development:
Collaboration Made Easy: Multiple developers can work on the same project simultaneously. Git intelligently merges changes and helps resolve conflicts when they arise.
Complete History Tracking: Every change made to your project is recorded with information about who made it, when it was made, and why. This audit trail is invaluable for understanding how your project evolved.
Branching and Experimentation: Git's lightweight branching system allows you to create separate lines of development. You can experiment with new features without affecting the main codebase, then merge successful changes back in.
Backup and Recovery: Since every developer has a complete copy of the repository, your project is automatically backed up across multiple machines. If something goes wrong, you can easily restore previous versions of your code.
Integration with Development Tools: Git integrates seamlessly with platforms like GitHub, GitLab, and Bitbucket, providing additional features for code review, project management, and continuous integration.
Before diving into commands, let's understand the fundamental concepts that form the foundation of Git:
Repository (Repo): A repository is a storage space for your project. It contains all your files, folders, and the complete history of changes. Repositories can exist locally on your computer or remotely on platforms like GitHub.
Commit: A commit is a snapshot of your project at a specific point in time. Each commit records what changes were made, who made them, and when. Commits create a timeline of your project's development.
Branch: A branch is an independent line of development. The default branch is typically called main or master. You can create new branches to work on features or fixes without affecting the main codebase.
Head: HEAD is a pointer that refers to the current commit you're working on. It typically points to the latest commit in your current branch.
Staging Area (Index): The staging area is a middle ground between your working directory and the repository. It allows you to prepare and review changes before committing them.
Working Directory: This is your local project folder where you edit files. Changes here aren't tracked until you add them to the staging area.
Remote: A remote is a version of your repository hosted on the internet or another network. The most common remote is called origin.
Now let's explore the essential Git commands that you'll use in your daily workflow. These commands form the foundation of working with Git.
Before you start using Git, configure your identity:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
To start tracking a project with Git:
git init
This creates a new Git repository in your current directory, setting up all the necessary files for version control.
To create a local copy of an existing remote repository:
git clone https://github.com/username/repository.git
This downloads the entire repository, including all files and commit history.
To see which files have been modified, staged, or are untracked:
git status
This command is your best friend. Use it frequently to understand the current state of your repository.
To stage specific files for commit:
git add filename.txt
To stage all changes in the current directory:
git add .
To save your staged changes to the repository:
git commit -m "Add descriptive commit message"
Always write clear, descriptive commit messages that explain what changes you made and why.
To see the history of commits:
git log
For a more compact view:
git log --oneline
To create a new branch:
git branch feature-branch
To switch to a different branch:
git checkout feature-branch
To create and switch to a new branch in one command:
git checkout -b feature-branch
To merge changes from another branch into your current branch:
git merge feature-branch
To add a remote repository:
git remote add origin https://github.com/username/repository.git
To push your changes to the remote repository:
git push origin main
To fetch and merge changes from the remote repository:
git pull origin main
Let's walk through a typical workflow that demonstrates how these commands work together in practice.
# Create a new directory for your project
mkdir my-project
cd my-project
# Initialize Git
git init
# Create your first file
echo "# My Project" > README.md
# Check status
git status
# Add the file to staging area
git add README.md
# Commit the change
git commit -m "Initial commit: Add README"
# Create and switch to a new feature branch
git checkout -b add-login-feature
# Make changes to your files
# ... edit your code ...
# Check what changed
git status
# Add all changes
git add .
# Commit your work
git commit -m "Add user login functionality"
# Switch back to main branch
git checkout main
# Merge the feature branch
git merge add-login-feature
# Delete the feature branch (optional)
git branch -d add-login-feature
# Add remote repository (first time only)
git remote add origin https://github.com/username/my-project.git
# Push your changes
git push -u origin main
# Later, to get updates from others
git pull origin main
The Git workflow can be visualized in three main stages:
Working Directory: This is where you make changes to your files. When you edit, create, or delete files, these changes exist only in your working directory.
Staging Area: When you use git add, you move changes from the working directory to the staging area. This is where you prepare your commit, allowing you to select exactly which changes to include.
Repository: When you use git commit, the staged changes are saved as a snapshot in your repository's history. This creates a permanent record of your changes.
This three-stage process gives you fine-grained control over what gets committed, allowing you to organize your work into logical, reviewable chunks.
As you begin your Git journey, keep these best practices in mind:
Commit Often: Make small, frequent commits rather than large, infrequent ones. This makes it easier to track changes and revert if needed.
Write Meaningful Commit Messages: Your future self and teammates will thank you. Describe what changed and why, not just what you did.
Use Branches: Never work directly on the main branch for new features. Branches allow you to experiment safely and keep your main codebase stable.
Pull Before You Push: Always pull the latest changes from the remote repository before pushing your work to avoid conflicts.
Review Before Committing: Use git status and git diff to review your changes before committing. This helps catch mistakes and ensures you're committing what you intend to.
Git is a powerful tool that becomes more intuitive with practice. Start by mastering these basic commands and workflows, and gradually explore more advanced features as you become comfortable. Remember, everyone makes mistakes with Git—the beauty is that Git usually provides a way to recover from them.
The key to learning Git is to use it consistently. Start a personal project, contribute to open source, or collaborate with friends. The more you practice, the more natural Git will become.
Happy coding, and may your commits always be meaningful!
Have questions about Git or want to share your learning experience? Drop a comment below!