What is the difference between 'git add' and 'git commit'?
In Git, 'git add' and 'git commit' are two fundamental commands often used in sequence to manage changes within your repository. While both are crucial for saving your work, they operate at different stages of the Git workflow, serving distinct purposes related to preparing and finalizing modifications.
Understanding `git add`
git add is the first step in recording your changes. It's used to move modifications from your working directory into the 'staging area' (also known as the index). The staging area is a temporary buffer where you can prepare a snapshot of your changes before they are permanently recorded in the repository. You can add individual files, specific changes within files, or all modified files.
- Purpose: To stage changes, preparing them for the next commit.
- Effect: Moves changes from the working directory to the staging area.
- State: Does not directly affect the repository's history.
- Specificity: Allows for granular control, letting you select exactly which changes (even parts of files) you want to include in the next snapshot.
git add <file_name>
# Stages a specific file
git add .
# Stages all changes in the current directory and its subdirectories
git add -p
# Interactively stages parts of modified files
Understanding `git commit`
git commit is the command that permanently records the staged changes into the repository's history. It takes the snapshot from the staging area and stores it as a new commit object. Each commit has a unique identifier (SHA-1 hash), a commit message describing the changes, the author, and a timestamp. Commits are the building blocks of your project's history in Git.
- Purpose: To permanently save the staged changes into the repository's history.
- Effect: Creates a new commit object containing the staged snapshot and a commit message.
- State: Affects the repository's history, creating a new point in time.
- Dependency: Can only commit changes that have first been moved to the staging area via
git add.
git commit -m "Descriptive commit message"
# Creates a new commit with the staged changes and a message
git commit
# Opens a text editor to write a multi-line commit message
Key Differences Summarized
| Feature | `git add` | `git commit` |
|---|---|---|
| **Purpose** | Stages changes for the next commit | Permanently saves staged changes to the repository |
| **Effect** | Moves changes to the staging area (index) | Creates a new snapshot (commit) in the project history |
| **Location of Changes** | Working directory → Staging area | Staging area → Repository (.git directory) |
| **History Impact** | No direct impact on repository history | Creates a new point in the repository's history |
| **Granularity** | Allows selecting specific files/changes to include | Commits *all* changes currently in the staging area |
| **Workflow Stage** | Preparation/Staging | Recording/Saving |
In summary, git add is like preparing a package for shipment – you select and organize the items you want to send. git commit is like sealing that package and sending it off, creating a permanent record of what was inside at that moment. Both are integral to Git's version control model, allowing for fine-grained control over what gets saved and when.