How does 'git add' differ from 'git commit'?

QuestionsQuestions8 SkillsProYour First Git LabOct, 31 2025
0122

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

  1. Purpose:

    • The git add command is used to stage changes in your working directory. It prepares specific files or changes to be included in the next commit.
  2. 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.
  3. Effect:

    • Running git add does not create a new commit; it simply marks changes to be included in the next commit.
  4. Usage:

    • Example to stage a specific file:
      git add file-name
    • Example to stage all changes:
      git add .

git commit

  1. Purpose:

    • The git commit command is used to create a new commit in the repository. It records the staged changes along with a commit message describing those changes.
  2. 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.
  3. Effect:

    • Running git commit creates a new commit with a unique identifier (hash) and adds it to the commit history. This action cannot be undone without additional commands (like git revert or git reset).
  4. Usage:

    • Example to create a commit with a message:
      git commit -m "Your commit message here"

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.

0 Comments

no data
Be the first to share your comment!