Git Basics - init, clone, add, commit, status, log (Complete Guide)

Master Git basics! Learn git init, clone, add, commit, status, and log - your first Git workflow!

Introduction

Let's learn the essential Git commands! We'll create a repo, make commits, and view history!

What You Will Learn

  • git init: Create a new repo!
  • git clone: Copy an existing repo!
  • git add: Stage changes!
  • git commit: Save changes!
  • git status: Check repo status!
  • git log: View history!

Prerequisites

The Git Three States!

First, understand Git's three areas:

graph LR
    A[Working Directory<br/>Your files] -->|git add| B[Staging Area<br/>Index]
    B -->|git commit| C[Repository<br/>.git folder]
  1. Working Directory: Your actual files on disk!
  2. Staging Area: What's going to be in your next commit!
  3. Repository: The .git folder, all history!

Command 1: git init (Create a New Repository!)

Let's create a new repo!

# Create a folder (or use an existing one)
mkdir my-first-git-repo
cd my-first-git-repo

# Initialize Git repo!
git init

Great! You just created a Git repo! It has a hidden .git folder!

Command 2: git clone (Copy an Existing Repository!)

If you want to copy a repo from GitHub/GitLab:

git clone https://github.com/someone/some-repo.git

This downloads a full copy of the repo!

Command 3: git status (Check Your Repo Status!)

Your most-used Git command! Run it often!

git status

This tells you:

  • What branch you're on
  • What files have changed
  • What's staged
  • What's not tracked

Let's Make Our First Commit!

Let's create a file and commit it!

Step 1: Create a file

Create a README.md file:

# My First Git Repo
Hello Git!

Step 2: Check status

git status

You'll see README.md as untracked!

Step 3: Stage the file (git add!)

# Stage a single file
git add README.md

# OR stage ALL changed files
git add .

Now git status shows it's staged!

Step 4: Commit! (git commit!)

git commit -m "Initial commit: Add README.md"

Important: Always write a good commit message!

Command 4: git log (View History!)

See your commit history!

git log

You'll see:

  • Commit hash
  • Author
  • Date
  • Commit message

Git Basic Workflow Recap!

The standard Git workflow:

  1. Modify files!
  2. git status to check!
  3. git add to stage!
  4. git commit to save!
  5. Repeat!
flowchart TD
    A[Modify Files] -->|git status| B[Check Status]
    B -->|git add| C[Stage Files]
    C -->|git commit -m "msg"| D[Commit]
    D --> A

Command 5: git diff (See What Changed!)

See changes between working directory, staging, and last commit!

# See unstaged changes
git diff

# See staged changes
git diff --staged

Best Practices for Git Basics

  1. Commit often: Small, focused commits!
  2. Write good messages: Clear and concise!
  3. Check status before committing: Always!
  4. Don't commit secrets/API keys: Never ever!
  5. Use .gitignore: Ignore files you don't want to track!

Common Mistakes

  1. Forgot to git add before git commit: git status is your friend!
  2. Committing too much: Keep commits small and focused!
  3. Bad commit messages: "Fix bug" is bad! "Fix login error for mobile users" is good!

Interview Insights

Interviewers love to ask about Git basics and workflow!

Summary

You learned the essential Git commands: init, clone, status, add, commit, log, diff! You know the three Git states!

Related Articles

Previous Tutorial

Git Installation & Setup

Next Tutorial

Let's learn about commits and history! → Commits & History