iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

git commit

git commit is the act of recording state. Stage, write a message, commit. Then squash, amend, or split as the history demands.

Git — committing

EXAMPLE
# ===== The basic loop =====
git status                # what changed
git add file.ts           # stage specific
git add -p                # stage hunks interactively
git add .                 # stage everything (use sparingly)
git commit -m 'fix: handle empty cart'
git log --oneline -5

# ===== Multi-line message (the right way) =====
git commit                # opens $EDITOR
# OR via heredoc:
git commit -m "feat(orders): add coupon support" -m "

Allow OrderService.applyCoupon() and propagate to the read model."

# ===== Convention =====
# Conventional Commits format:
#   <type>(<scope>): <subject>
#   <blank line>
#   <body>
#   <blank line>
#   <footer>
# type: feat, fix, docs, chore, refactor, test, build, ci, perf
# scope: optional area
# Body explains the WHY; footer carries Co-Authored-By, BREAKING CHANGE notes

# ===== Atomic commits =====
# One commit = one logical change.
# Split unrelated work; avoid 'wip' / 'misc' commits.
# Easier to review, easier to revert, easier to find with git bisect.

# ===== Amending the last commit =====
git add forgotten-file.ts
git commit --amend           # opens editor for message
git commit --amend --no-edit # keep message, just update files
# WARNING: rewrites history; never amend AFTER pushing to a shared branch.

# ===== Splitting a commit =====
# Reset to before it, then commit pieces:
git reset --soft HEAD~1    # uncommit, keep staged
git reset                  # unstage; files still in working tree
git add part1; git commit -m 'first piece'
git add part2; git commit -m 'second piece'

# ===== Fixup + autosquash =====
git commit --fixup=<sha>            # marks a commit as a fix for an earlier one
git rebase -i --autosquash main     # reorders the fixup right after its target

# ===== Sign + verify =====
# Configure GPG or SSH signing once:
git config --global commit.gpgsign true
git config --global user.signingkey <key>
git commit -S -m '...'

# ===== Inspect =====
git show HEAD
git show <sha>
git show HEAD --stat
git log -p src/handlers.ts        # patches that touched the file
git log --grep='feat'

# ===== Patterns to internalise =====
# - Stage hunks with -p; one commit per concern
# - Conventional Commits + meaningful body lines
# - Amend / fixup ONLY before push
# - Sign commits where the team requires it

# ===== Pitfalls =====
# - 'git commit -am' on a repo with new files (only adds tracked changes)
# - Force-pushing amended commits to shared branches
# - Tiny 'wip' commits that pollute history
# - Reformatting + logic changes in the same commit

Why it matters

A commit is a unit of intent. Stage thoughtfully (add -p), write the why in the body, keep changes atomic, and use amend / fixup before publishing. The discipline pays back forever in code reviews, git bisect, and the ability to revert a single change without unwinding ten.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
git commit -m 'feat: add login form'
git commit --amend   # edit the last commit
git commit -a -m 'msg'   # stage modified files & commit
Try it Yourself »

Exercise

Commit with an inline message.

git commit 'fix typo'

Test yourself

Q1. Commit with an inline message via…
Q2. Edit the latest commit with…
Q3. Stage modified files + commit in one shot via…

Discussion

Loading…