The git add and git commit commands serve different purposes in the Git version control system. Here’s a breakdown of their differences:
git add
-
Purpose:
- The
git addcommand is used to stage changes in your working directory. It prepares specific files or changes to be included in the next commit.
- The
-
Functionality:
- It can stage new files, modified files, or even deletions.
- You can selectively stage changes, allowing you to choose which modifications to include in the next commit.
-
Effect:
- Running
git adddoes not create a new commit; it simply marks changes to be included in the next commit.
- Running
-
Usage:
- Example to stage a specific file:
git add file-name - Example to stage all changes:
git add .
- Example to stage a specific file:
git commit
-
Purpose:
- The
git commitcommand is used to create a new commit in the repository. It records the staged changes along with a commit message describing those changes.
- The
-
Functionality:
- It takes all the changes that have been staged (using
git add) and saves them as a new snapshot in the repository's history.
- It takes all the changes that have been staged (using
-
Effect:
- Running
git commitcreates a new commit with a unique identifier (hash) and adds it to the commit history. This action cannot be undone without additional commands (likegit revertorgit reset).
- Running
-
Usage:
- Example to create a commit with a message:
git commit -m "Your commit message here"
- Example to create a commit with a message:
Summary
git add: Stages changes for the next commit; does not create a commit.git commit: Creates a new commit from the staged changes; records the changes in the repository's history.
In practice, you typically use git add to stage your changes and then git commit to save those changes to the repository.
