Understanding Git Commits
What is a Git Commit?
A Git commit is a fundamental operation in version control that captures a snapshot of your project's changes at a specific point in time. When you create a commit, you're essentially saving a set of modifications to your repository with a descriptive message explaining what changes were made.
Core Commit Workflow
graph LR
A[Working Directory] --> B[Staging Area]
B --> C[Git Repository]
C --> D[Commit History]
Basic Commit Commands
Command |
Description |
Usage |
git add |
Stage changes |
git add file.txt |
git commit |
Create a commit |
git commit -m "Descriptive message" |
git commit -a |
Stage and commit modified files |
git commit -a -m "Quick update" |
Practical Example
Let's demonstrate a typical commit workflow on Ubuntu 22.04:
## Initialize a new git repository
mkdir project
cd project
git init
## Create a sample file
echo "Hello, Git Commits!" > README.md
## Stage the file
git add README.md
## Create a commit
git commit -m "Initial project setup"
## View commit details
git log
Commit Anatomy
Each Git commit contains:
- Unique SHA-1 hash identifier
- Author information
- Timestamp
- Commit message
- Pointer to previous commit
- Snapshot of project state
Key Characteristics
Git commits are immutable snapshots that provide:
- Version tracking
- Collaborative development
- Rollback capabilities
- Project history documentation