Skip to main content

Command Palette

Search for a command to run...

Git Methods: Basic Snapshotting

Published
4 min read
Git Methods: Basic Snapshotting
M

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).

Mental model

Git tracks three key areas:

Working tree  — your files on disk
Index (staging area) — what will go into the next commit
HEAD — the last commit (current snapshot)

Typical flow:

edit files → git add → git commit

Use git status and git diff to see exactly where changes live.

1) git status — working directory dashboard

What it shows

  • Untracked files (new, not staged)

  • Changes not staged for commit (modified in working tree)

  • Changes to be committed (staged in index)

  • Current branch, ahead/behind info

Useful options

  • git status -s (short)

  • git status --porcelain (script-friendly, stable format)

Notes

  • Run this frequently; it’s the safest way to orient yourself.

2) git add — stage changes

Purpose

  • Move changes from working tree → index.

Common usage

  • git add <path> — stage a file or folder

  • git add . — stage changes in current directory (new/modified, not removals)

  • git add -A — stage all changes (adds, modifications, deletions)

  • git add -u — stage modifications and deletions (no new files)

  • git add -p — interactively stage hunks (fine control)

Notes

  • Respect .gitignore rules.

  • If you accidentally staged too much, see git reset <path> below.

3) git diff — inspect changes

Working tree vs index

  • git diff — shows unstaged changes (working tree → index)

Index vs last commit (HEAD)

  • git diff --staged (or --cached) — shows what’s staged for the next commit

Between commits/branches

  • git diff HEAD~1..HEAD

  • git diff main..feature/login

Useful flags

  • --name-only — list changed paths

  • --word-diff — word-level changes (helpful for prose/code)

  • --stat — summary of added/removed lines by file

4) git commit — record a snapshot

Basic

  • git commit -m "Message" — commit what’s staged

  • git commit — opens editor for a multi-line message

Quality-of-life

  • git commit -a -m "Message" — stage and commit all modified tracked files (doesn’t add new files)

  • git commit --amend — edit the last commit (message and/or content)

    • Add more files then: git commit --amend --no-edit

    • Safe only if that commit hasn’t been pushed/shared

Good practice

  • Write concise, imperative messages (e.g., “Add contact form validation”).

  • Commit logically grouped changes.

5) git reset — unstage or move history pointer

Unstage files (keep edits)

  • git reset <path> — move paths out of index back to working tree
    (Modern equivalent: git restore --staged <path>)

Move HEAD (and optionally index/working tree)

  • git reset --soft <commit> — move HEAD; keep index & working tree

  • git reset --mixed <commit> (default) — move HEAD; reset index; keep working tree

  • git reset --hard <commit> — move HEAD; reset index & working tree (discard changes)

Examples

  • git reset HEAD~1 — undo last commit but keep changes (unstaged)

  • git reset --hard HEAD — discard all working/index changes (dangerous)

Safety tip

  • Prefer git revert <commit> to undo public history; reset rewrites history and can disrupt collaborators.

6) git rm — remove files

Remove from repo and disk

  • git rm <path>; then git commit -m "Remove <path>"

Stop tracking but keep the file locally

  • git rm --cached <path>

  • Then add to .gitignore if it should stay untracked

Notes

  • If you deleted a file manually, you can stage the deletion with git add -A or git rm <path>.

7) git mv — move/rename files

Basic

  • git mv old.txt new.txt

  • git mv notes/ readme/notes.md docs/notes.md (moves + renames)

Notes

  • Equivalent to OS move + git add -A, but git mv is simpler.

  • Git can detect renames even without git mv, based on content similarity.

Quick recipes

  • See what changed and where:

      git status
      git diff           # unstaged
      git diff --staged  # staged vs HEAD
    
  • Stage precisely what you want:

      git add -p file.py
    
  • Commit and fix last commit:

      git commit -m "Implement feature X"
      # add forgotten file
      git add missing.cfg
      git commit --amend --no-edit
    
  • Unstage without losing edits:

      git reset path/to/file
      # or: git restore --staged path/to/file
    
  • Discard local changes to a file (danger: cannot be easily recovered):

      git checkout -- path/to/file           # older Git
      # or modern:
      git restore path/to/file
    
  • Remove tracked file but keep it on disk:

      git rm --cached secrets.json
      echo "secrets.json" >> .gitignore
      git commit -m "Stop tracking secrets.json"
    
  • Rename/move and commit:

      git mv README.txt README.md
      git commit -m "Rename README to .md"
    

Cheat sheet (copy/paste)

# Inspect
git status
git diff
git diff --staged

# Stage
git add <path>        # file/folder
git add -A            # add/modify/delete
git add -p            # interactive hunks

# Commit
git commit -m "Message"
git commit -a -m "Message"
git commit --amend
git log --oneline --graph --decorate --all

# Unstage / reset
git reset <path>                # unstage file(s)
git reset --soft HEAD~1         # keep index & worktree
git reset HEAD~1                # unstage, keep edits
git reset --hard HEAD~1         # discard everything to that commit (danger)

# Remove / move
git rm <path>
git rm --cached <path>
git mv old new

.

1 views