What is the difference between add and commit in Git?
In Git, “add” and “commit” are two separate commands with different functions and usage.
- The add command is used to add file modifications or additions to the staging area, telling Git to track these changes without saving them to the repository. After adding modifications to the staging area with the add command, you can use the commit command to save these changes to the repository. The basic syntax for using the add command is as follows:
- Add the specified file to the staging area using “git add
“, or add all modified files to the staging area using “git add .”. - The commit command is used to save changes from the staging area to the repository, permanently storing them as a new version. It generates a unique commit object containing information such as the author, time of the commit, description, and parent commit. The basic syntax for using the commit command is as follows:
- git commit -m “commit message” # Commit files in staging area and add commit message
In general, the add command is used to add changes to the staging area, while the commit command is used to submit the changes from the staging area to the repository. The add command is a preparation stage for the changes to be submitted, while the commit command is an execution stage for carrying out the submission.