Git commit
A Git commit is a core concept in Git, representing a snapshot of your project's file changes at a specific point in time. Each commit acts as a save point or checkpoint in the version history of a Git repository, allowing you to revisit, revert, or explore the project’s development over time. Here's a deeper explanation:
Key Features of Git Commits:
Snapshot of Changes:
- A commit captures a snapshot of the current state of the project’s tracked files.
- Unlike other version control systems that store file differences (deltas), Git stores the entire snapshot of the files, although it optimizes storage by reusing unchanged files between commits.
Commit Message:
- Each commit is associated with a message describing what changes were made and why.
- This message is important for understanding the context of the changes and helps during code reviews or debugging.
Commit Hash (SHA-1):
- Every commit is identified by a unique 40-character string (called a hash or SHA-1 checksum), which looks something like this:
e7c3e8a1f7b11a4d1b7c838f2f2a6fa4b9c9e1e5
. - This hash ensures that the commit is globally unique, and it allows you to reference specific commits.
- Every commit is identified by a unique 40-character string (called a hash or SHA-1 checksum), which looks something like this:
Commit Tree:
- Git organizes commits in a tree-like structure. Each commit points back to its parent commit(s), forming a chain or history of commits.
- For example, in a linear history, every commit points to one parent, but in the case of merges, a commit can have multiple parent commits.
Atomic:
- Git commits are atomic, meaning that either all changes in the commit are applied, or none are. This prevents partial updates and ensures a consistent history.
Life Cycle of a Git Commit:
Working Directory:
- This is where you modify your project files. Changes made here are unstaged and not yet tracked by Git for a commit.
Staging Area (Index):
- Before committing, you move your changes to the staging area using the
git add
command. - The staging area allows you to control which changes will be included in the next commit.
- Before committing, you move your changes to the staging area using the
Committing:
- When you run
git commit -m "message"
, Git creates a new commit, saving the changes in the staging area along with the commit message and associating it with your project history.
- When you run
Common Commands Related to Commits:
git add <file>
: Adds the changes of a file to the staging area in preparation for committing.git commit -m "message"
: Creates a commit with the changes in the staging area and attaches the specified message.git commit --amend
: Edits the last commit (e.g., to fix a mistake in the commit message or add forgotten changes).git log
: Shows a history of commits, including their hash, author, date, and message.git reset <commit>
: Moves the current branch pointer to a specific commit, either soft (keeping changes in the working directory) or hard (discarding changes).git revert <commit>
: Creates a new commit that undoes the changes made in a specific commit.
Anatomy of a Commit:
Each commit consists of:
- A unique commit hash: Identifies the commit.
- Author information: Name and email of the person who made the commit.
- Date and time: When the commit was created.
- Commit message: Describes the purpose of the commit.
- Parent commits: Reference(s) to the previous commit(s) (or none for the first commit).
- Tree object: Represents the snapshot of the project’s files at the time of the commit.
Example:
$ git commit -m "Added login feature"
This command commits all staged changes with the message "Added login feature". From this point forward, Git tracks this commit and any new changes.
Benefits of Commits:
- Version History: Every commit preserves a historical record of the project's evolution, making it easy to review and compare previous states.
- Collaboration: Multiple contributors can commit changes, which can then be merged, allowing for smooth team collaboration.
- Reverting Changes: Commits allow you to revert back to previous versions or undo specific changes if a bug or issue is introduced.
Git commits are a powerful way to manage code development, giving you full control over version history and the ability to work collaboratively with others while maintaining code integrity.