Linux and Command-Line Basics
After editing main.c, a file managed with Git, only git commit -m "fix" was run, and "no changes added to commit" was shown and nothing was committed. Which of the following is the reason?
- ABefore committing, the changes have to be sent to the shared repository with git push
- BChanges to be included in a commit must first be registered in the staging area with git add
- CGit commits automatically when a file is saved, so there is no need to run git commit
Show answer and explanation
Answer: B. Changes to be included in a commit must first be registered in the staging area with git add
In Git, of the changes edited in the working directory, only those registered in the staging area (the index) with git add are recorded by the next git commit. This two-step mechanism lets you pick only related changes out of several edits and make them one commit. git status shows which changes are staged. There is also git commit -a, which stages all changes to tracked files and commits them at once.
- A. git push sends commits to the shared repository and is used after committing.
- B. Correct. Only changes staged with git add are committed.
- C. Git does not commit automatically. The user decides which changes to record and when, with git add and git commit.