← Back to Chapters

Git Commit

? Git Commit

? Quick Overview

Committing is one of the most important operations in Git. It allows you to save your changes to the local repository and build a clear history of your work.

? Key Concepts

  • A commit is a snapshot of your project at a specific time
  • Each commit has a unique hash ID
  • Only staged changes are included in a commit

? What is a Commit?

A commit in Git records the current state of staged files. It includes the changes along with a message explaining what and why the change was made.

⌨️ Basic Commit Command

? View Code Example
// Commit staged changes with a short message
git commit -m "Your commit message"

? Commit with a Detailed Message

? View Code Example
// Opens default editor for a detailed commit message
git commit

♻️ Amending the Last Commit

? View Code Example
// Modify the last commit or add missing changes
git commit --amend

? Viewing Commit History

? View Code Example
// View commit history of the current branch
git log

? Viewing Changes in a Commit

? View Code Example
// Show changes introduced by a specific commit
git show <commit-hash>

⏪ Rolling Back a Commit

? View Code Example
// Undo last commit but keep changes staged
git reset --soft HEAD~1
? View Code Example
// Completely discard last commit and changes
git reset --hard HEAD~1

? Interactive Commit Flow

? Edit File → ➕ git add → ? git commit → ? History Updated

?️ Use Cases

  • Saving progress checkpoints
  • Tracking changes over time
  • Collaborating safely with teams

? Tips & Best Practices

  • Write clear and meaningful commit messages
  • Commit small logical changes
  • Avoid amending commits that are already pushed

? Try It Yourself

  • Modify a file and stage it using git add
  • Commit with a meaningful message
  • Amend the commit to fix a mistake
  • Explore commit history using git log