Git

Basic Workflow

Git · 10 entries

Check Repository Status

syntax
git status [-s]
example
git status
git status -s
output
On branch main
Changes not staged for commit:
  modified:   app.js
Untracked files:
  utils.js

# Short format:
 M app.js
?? utils.js

Note The short flag (-s) gives a compact two-column output: left column = staging area, right column = working tree. M = modified, A = added, ? = untracked, D = deleted.

Stage Changes

syntax
git add <file|pattern|.>
example
git add app.js
git add src/
git add *.ts
git add -A

Note git add . stages new and modified files in the current directory and below. git add -A stages everything including deletions from anywhere in the repo. Prefer staging specific files to avoid accidentally committing secrets or large binaries.

Stage Part of a File

syntax
git add -p [file]
example
git add -p utils.js
# Git shows each hunk and asks:
# Stage this hunk [y,n,q,a,d,s,e,?]?
# y = stage, n = skip, s = split into smaller hunks

Note Extremely useful when a file has multiple unrelated changes. You can commit them separately for a cleaner history. Use 's' to split a hunk if Git grouped too many changes together.

Commit Staged Changes

syntax
git commit [-m "message"]
example
git commit -m "Add user authentication endpoint"

# Multi-line message:
git commit -m "Fix cart total calculation

The discount was applied after tax instead of before.
Updated the order of operations in calculateTotal()."
output
[main a1b2c3d] Add user authentication endpoint
 2 files changed, 45 insertions(+), 3 deletions(-)

Note Without -m, Git opens your configured editor for the message. A good commit message has a short summary line (under 72 chars), a blank line, then optional details.

Stage & Commit in One Step

syntax
git commit -am "message"
example
git commit -am "Update API response format"
output
[main f4e5d6c] Update API response format
 1 file changed, 12 insertions(+), 8 deletions(-)

Note The -a flag only stages already-tracked files that have been modified or deleted. It will NOT add new untracked files. You still need git add for brand new files.

View Unstaged Changes

syntax
git diff [file]
git diff --staged [file]
example
# Changes in working tree (not yet staged):
git diff

# Changes that are staged for the next commit:
git diff --staged

# Diff for a specific file:
git diff src/auth.js
output
diff --git a/src/auth.js b/src/auth.js
--- a/src/auth.js
+++ b/src/auth.js
@@ -10,3 +10,5 @@
+  const token = generateToken(user);
+  return { token, expiresIn: 3600 };

Note Plain git diff shows only unstaged changes. Use --staged (or --cached, they're identical) to see what will go into the next commit. If git diff shows nothing, your changes are probably already staged.

View Commit History

syntax
git log [options]
example
git log
git log --oneline
git log --oneline -10
git log --since='2 weeks ago'
git log --author='Samira'
output
a1b2c3d Add user authentication endpoint
f4e5d6c Update API response format
9g8h7i6 Initial commit

Note git log shows most recent commits first. Use -n or -<number> to limit output. Press q to exit the pager if the log is long.

Inspect a Specific Commit

syntax
git show <commit-hash|HEAD|tag>
example
git show a1b2c3d
git show HEAD
git show HEAD~2
output
commit a1b2c3d...
Author: Samira Khan <samira@example.com>
Date:   Mon Mar 30 14:22:01 2026 +0000

    Add user authentication endpoint

diff --git a/src/auth.js b/src/auth.js
...

Note HEAD~2 means 'two commits before HEAD'. You can also show specific files from a commit: git show HEAD:src/app.js

Summarize Commits by Author

syntax
git shortlog [-s] [-n] [-e]
example
git shortlog -sne
git shortlog --since='1 month ago'
output
    23  Samira Khan <samira@example.com>
    15  Dev Patel <dev@example.com>
     7  Aiko Tanaka <aiko@example.com>

Note -s shows only counts (no commit messages), -n sorts by number of commits (most first), -e includes email addresses. Handy for seeing who contributed most.

Diff Summary Statistics

syntax
git diff --stat [branch|commit]
example
git diff --stat main
git diff --stat HEAD~5
output
 src/auth.js   | 12 ++++++--
 src/routes.js |  4 ++--
 2 files changed, 10 insertions(+), 4 deletions(-)

Note Shows a compact summary of which files changed and how many lines were added/removed. Combine with --staged to see staged changes summary.