Home/Learn/Git/Core Workflow — init, add, commit, log

Core Workflow — init, add, commit, log

Beginner
Beginner

The daily Git loop: initialise a repo, stage changes precisely with git add, commit with a meaningful message, and explore history with git log. Mastering these five commands covers 80% of day-to-day Git usage.

Overview

Most developers rush past the fundamentals and end up with messy commit histories, vague messages, and no discipline around what goes into each commit. A clean Git history is documentation — future-you and your teammates will read it when debugging a production issue at 2am. This concept covers the complete daily workflow with best practices: when to commit, how to write good messages, how to use git add -p to make surgical commits, and how to read history effectively.

Initialising and Cloning

Start a new project with git init, which creates a .git directory. Clone an existing project with git clone — this copies the full history, not just the latest code. The remote is automatically named "origin".

Initialise or clone
# Start a new project
mkdir my-project && cd my-project
git init
# Initialised empty Git repository in .git/

# Clone an existing repository (full history included)
git clone https://github.com/user/project.git
git clone https://github.com/user/project.git my-folder  # custom name

# After cloning, the remote is already configured:
git remote -v
# origin  https://github.com/user/project.git (fetch)
# origin  https://github.com/user/project.git (push)

Staging Changes Precisely

git add does not mean "save this file" — it means "include this in my next commit". The staging area lets you craft surgical commits even if you changed 10 files. git add -p (patch mode) is the most powerful workflow: it shows each change hunk and asks y/n/s (split), so one file can be partially staged.

Precision staging with git add -p
# See what changed
git status             # summary
git diff               # unstaged changes (working dir vs stage)
git diff --staged      # staged changes (stage vs last commit)

# Stage specific files
git add src/auth.js
git add src/          # everything in a directory

# Stage specific hunks interactively (most powerful)
git add -p
# diff --git a/auth.js b/auth.js
# @@ -12,6 +12,10 @@
# +function validateEmail(email) {
# +  return /S+@S+.S+/.test(email)
# +}
# Stage this hunk [y,n,q,a,d,s,?]? y

# Remove a file from staging (keep changes in working dir)
git restore --staged src/auth.js

# Discard all changes in working directory (CAREFUL — permanent)
git restore src/auth.js

Committing with Good Messages

A commit message is a letter to your future self. The subject line (≤72 chars) answers "what changed". The body (separated by blank line) answers "why". Use the imperative mood: "Add login validation" not "Added" or "Adding".

Writing good commit messages
# Simple commit (subject only)
git commit -m "Add email validation to signup form"

# Full commit message with body (use the editor)
git commit
# Opens $EDITOR with template:
# Add email validation to signup form
#
# Users could previously submit any string as email, causing
# silent failures in the notification pipeline. This adds a
# regex check at the controller layer and returns 400 with
# a descriptive error message.
#
# Closes #142

# Amend the last commit (before pushing)
git commit --amend -m "Better message"
git commit --amend --no-edit  # keep message, just change content

# Good message prefixes (Conventional Commits)
# feat:     new feature
# fix:      bug fix
# docs:     documentation only
# refactor: code change, no feature/fix
# test:     adding tests
# chore:    build process, tooling

Reading History with git log

git log is your time machine. Learn to filter and format it — the default output is rarely what you want.

Reading history effectively
# One-line summary (most common)
git log --oneline
# a3f8c12 Add payment gateway integration
# 9d2e441 Fix null pointer in checkout flow
# 3b7f920 Implement shopping cart

# Visual branch graph
git log --oneline --graph --all
# * a3f8c12 (HEAD -> main) Add payment gateway
# | * 7b2e441 (feature/cart) Add cart animation
# |/
# * 9d2e441 Fix null pointer
# * 3b7f920 Initial commit

# Filter by author, date, or message
git log --author="Akshay"
git log --since="2 weeks ago"
git log --grep="payment"

# Show what changed in each commit
git log -p
git log --stat          # files changed + line counts

# Who last touched each line?
git blame src/auth.js

Key Points to Remember

  • 1git add stages changes — it controls exactly what goes into the next commit, not just which files
  • 2git add -p (patch mode) lets you stage individual hunks — make surgical, single-purpose commits
  • 3Write commit messages in imperative mood: "Fix bug" not "Fixed bug" or "Fixing bug"
  • 4git diff shows unstaged changes; git diff --staged shows what is about to be committed
  • 5git log --oneline --graph --all is the best quick overview of branch history

Interview Questions

Sign in to ask Aria
1

What is the difference between git add and git commit?

Easy
2

How would you stage only part of a file's changes in a single commit?

Medium
3

What makes a good Git commit message? What is the Conventional Commits specification?

Medium

Ask Aria about Core Workflow — init, add, commit, log

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…