← Back to Chapters

Git Commit Amend

? Git Commit Amend

? Quick Overview

The git commit --amend command allows you to modify the most recent commit. It helps fix small mistakes like incorrect commit messages or missing files without creating an additional commit.

? Key Concepts

  • Amend modifies only the most recent commit
  • A new commit hash is generated
  • History is rewritten
  • Best used before pushing to remote

? Syntax & Theory

? View Code Example
// Opens editor to modify the last commit message
git commit --amend

✏️ Change Commit Message Only

? View Code Example
// Keeps content same but allows message update
git commit --amend --no-edit

➕ Add Missed Files

? View Code Example
// Stage missing file before amending
git add file-name
? View Code Example
// Adds staged changes to last commit
git commit --amend --no-edit

? What Happens Internally?

Explanation

Git creates a brand-new commit with a different SHA-1 hash. The old commit is replaced, which means commit history changes.

? Interactive Understanding

Think of amend as replacing the top card in a stack instead of adding a new one.

HEAD → New Commit

? Use Cases

  • Fixing typo in commit message
  • Adding forgotten configuration files
  • Correcting small logic changes

⚠️ Amended Commit After Push

? View Code Example
// Force push updated commit to remote
git push --force

? Tips & Best Practices

  • Use only for local commits
  • Avoid amending shared commits
  • Communicate before force-pushing

? Try It Yourself

  • Create a commit and amend its message
  • Add a missed file using amend
  • Test --no-edit behavior