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]
- Working Directory: Your actual files on disk!
- Staging Area: What's going to be in your next commit!
- 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:
- Modify files!
git statusto check!git addto stage!git committo save!- 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
- Commit often: Small, focused commits!
- Write good messages: Clear and concise!
- Check status before committing: Always!
- Don't commit secrets/API keys: Never ever!
- Use .gitignore: Ignore files you don't want to track!
Common Mistakes
- Forgot to
git addbeforegit commit:git statusis your friend! - Committing too much: Keep commits small and focused!
- 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
Next Tutorial
Let's learn about commits and history! → Commits & History