DEV Community

Mohd Asif Ansari
Mohd Asif Ansari

Posted on

Git for Beginners: Basics and Essential Commands

If you're starting your coding journey, Git is one of the first tools you need to learn. It's not optional anymore it's as essential as knowing how to code itself.

Let's learn Git from scratch, in simple terms.

What is Git?

Git is a version control system. Think of it as a time machine for your code.

Simple explanation:

  • Git tracks every change you make to your files
  • You can go back to any previous version anytime
  • Multiple people can work on the same project without breaking things
  • Everything is saved with a complete history

Created by: Linus Torvalds (the creator of Linux) in 2005

Type: Distributed Version Control System (DVCS)

What "distributed" means: Every developer has the complete project history on their computer. If one computer breaks, nothing is lost.

Why Use Git?

1. Track Changes
Know exactly what changed, when, and why.

2. Undo Mistakes
Broke something? Go back to when it worked.

3. Collaborate Safely
Multiple people can work together without overwriting each other's code.

4. Experiment Freely
Try new ideas without fear. If it fails, just discard it.

5. Professional Standard
Every company uses Git. It's a must-have skill.

Core Concepts You Need to Know

Before jumping into commands, understand these basics:

Repository (Repo)

A folder that Git tracks. It contains your project and its entire history.

Two types:

  • Local repository: On your computer
  • Remote repository: On GitHub, GitLab, etc.

Commit

A snapshot of your project at a specific moment. Think of it as a save point in a video game.

Each commit has:

  • A unique ID (hash)
  • Author name
  • Date and time
  • A message describing what changed

Branch

A separate version of your code. Like parallel universes for your project.

Common branches:

  • main or master: The main version
  • feature-login: Working on login feature
  • bugfix-header: Fixing a header bug

Staging Area

A waiting room for changes before they're committed. You decide which changes to include in the next commit.

HEAD

A pointer that shows which commit you're currently on. Usually points to the latest commit on your current branch.

Working Directory

The actual files you see and edit on your computer.

The Three States of Git

Understanding this is crucial:

Working Directory → Staging Area → Repository

1. Modified:  You changed files but haven't saved them to Git
2. Staged:    You marked changed files to be committed
3. Committed: Changes are saved in Git history
Enter fullscreen mode Exit fullscreen mode

Flow:

  1. Edit files (Modified state)
  2. Add files to staging (Staged state)
  3. Commit files (Committed state)

Essential Git Commands

Let's learn the commands you'll use every day.

1. Setup Git (First Time Only)

Tell Git who you are:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

Check your settings:

git config --list
Enter fullscreen mode Exit fullscreen mode

2. Starting a New Project

Create a new Git repository:

# Navigate to your project folder
cd my-project

# Initialize Git
git init
Enter fullscreen mode Exit fullscreen mode

This creates a hidden .git folder that stores all Git data.

3. Checking Status

See what's happening in your repo:

git status
Enter fullscreen mode Exit fullscreen mode

This shows:

  • Which files changed
  • Which files are staged
  • Which branch you're on

Example output:

On branch main
Changes not staged for commit:
  modified:   index.html

Untracked files:
  style.css
Enter fullscreen mode Exit fullscreen mode

4. Adding Files to Staging

Add specific file:

git add index.html
Enter fullscreen mode Exit fullscreen mode

Add multiple files:

git add index.html style.css
Enter fullscreen mode Exit fullscreen mode

Add all changes:

git add .
Enter fullscreen mode Exit fullscreen mode

What this does: Moves files from "Modified" to "Staged" state.

5. Committing Changes

Save your staged changes:

git commit -m "Add homepage design"
Enter fullscreen mode Exit fullscreen mode

The -m flag adds a message describing what you did.

Good commit messages:

  • "Add user login feature"
  • "Fix navigation bar bug"
  • "Update contact page content"

Bad commit messages:

  • "Changes"
  • "Update"
  • "asdfgh"

6. Viewing History

See all commits:

git log
Enter fullscreen mode Exit fullscreen mode

Shorter version:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Example output:

a3b4c5d Add login page
f6e7d8c Fix header styling
b9c8d7e Initial commit
Enter fullscreen mode Exit fullscreen mode

7. Working with Branches

See all branches:

git branch
Enter fullscreen mode Exit fullscreen mode

Create a new branch:

git branch feature-login
Enter fullscreen mode Exit fullscreen mode

Switch to a branch:

git checkout feature-login
Enter fullscreen mode Exit fullscreen mode

Create and switch in one command:

git checkout -b feature-login
Enter fullscreen mode Exit fullscreen mode

Delete a branch:

git branch -d feature-login
Enter fullscreen mode Exit fullscreen mode

8. Merging Branches

Merge a branch into current branch:

# Switch to main branch
git checkout main

# Merge feature branch into main
git merge feature-login
Enter fullscreen mode Exit fullscreen mode

9. Connecting to Remote Repository

Add a remote repository:

git remote add origin https://github.com/username/repo.git
Enter fullscreen mode Exit fullscreen mode

Check remote connections:

git remote -v
Enter fullscreen mode Exit fullscreen mode

10. Pushing and Pulling

Push your code to remote:

git push origin main
Enter fullscreen mode Exit fullscreen mode

Pull latest code from remote:

git pull origin main
Enter fullscreen mode Exit fullscreen mode

First time push:

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

The -u flag sets up tracking.

11. Cloning a Repository

Download an existing repository:

git clone https://github.com/username/repo.git
Enter fullscreen mode Exit fullscreen mode

12. Other Useful Commands

See changes in files:

git diff
Enter fullscreen mode Exit fullscreen mode

Undo changes (not committed):

git restore index.html
Enter fullscreen mode Exit fullscreen mode

Unstage a file:

git restore --staged index.html
Enter fullscreen mode Exit fullscreen mode

See commit details:

git show commit-id
Enter fullscreen mode Exit fullscreen mode

Basic Developer Workflow

Here's how you'll use Git daily:

Starting Your Day

# 1. Pull latest changes
git pull origin main

# 2. Create a new branch for your task
git checkout -b feature-add-footer

# 3. Start coding!
Enter fullscreen mode Exit fullscreen mode

While Working

# 4. Check what changed
git status

# 5. Add your changes
git add .

# 6. Commit with a clear message
git commit -m "Add footer with social links"

# 7. Keep committing as you make progress
Enter fullscreen mode Exit fullscreen mode

Finishing Your Work

# 8. Push your branch to remote
git push origin feature-add-footer

# 9. Create a Pull Request on GitHub (done in browser)

# 10. After approval, merge and delete branch
git checkout main
git pull origin main
git branch -d feature-add-footer
Enter fullscreen mode Exit fullscreen mode

Common Mistakes to Avoid

1. Forgetting to commit regularly
Commit after each small change. Don't wait until end of day.

2. Bad commit messages
Write clear messages. Your future self will thank you.

3. Working directly on main
Always create a branch for new features.

4. Not pulling before pushing
Always pull latest changes first to avoid conflicts.

5. Adding everything blindly
Don't do git add . without checking git status first.

Quick Reference Cheat Sheet

# Setup
git config --global user.name "Name"
git config --global user.email "email"

# Start
git init                    # Create new repo
git clone <url>            # Copy existing repo

# Basic workflow
git status                 # Check status
git add <file>            # Stage file
git add .                 # Stage all files
git commit -m "message"   # Commit changes
git log                   # View history

# Branches
git branch                # List branches
git branch <name>         # Create branch
git checkout <name>       # Switch branch
git checkout -b <name>    # Create and switch
git merge <name>          # Merge branch

# Remote
git remote add origin <url>  # Add remote
git push origin <branch>     # Push to remote
git pull origin <branch>     # Pull from remote

# Undo
git restore <file>           # Undo changes
git restore --staged <file>  # Unstage file
Enter fullscreen mode Exit fullscreen mode

Next Steps

Now that you know Git basics:

  1. Practice daily: Use Git for every project
  2. Learn GitHub: Create an account and push your code
  3. Explore branching: Practice creating and merging branches
  4. Study conflicts: Learn how to resolve merge conflicts
  5. Read documentation: Git has excellent docs at git-scm.com

Final Thoughts

Git seems complicated at first, but it's just:

  • Make changes
  • Stage them
  • Commit them
  • Push them

That's 90% of what you'll do daily.

The other 10%? You'll learn as you go.

Start using Git today. Create a repo. Make commits. Push to GitHub. The best way to learn Git is by using it.


Resources:

  • Official Git documentation: git-scm.com
  • Interactive tutorial: learngitbranching.js.org
  • GitHub guides: guides.github.com

Top comments (0)