Edit and Commit Overview (2)
To prepare for the next chapter, we introduce another diagram to explain the edit and commit workflow. This illustration will help you understand how Git works with branches, which will be the focus of the next chapter. The following is the explanation of key icons.
Working Tree or Working Directory, which you see on your local computer for daily editing.
INDEX or Staging Area. If the Working Tree is inside this dotted circle, the edit status of the working tree and the staging area are the same.
Commit. If the Working Tree is inside this bold circle, the edit status of the Working Tree and the commit are the same.
In this diagram, you can see the progress of coding horizontally. If icons are on the same status column (e.g., Status B), the files or directories are in the same status too.
The steps below are the explanation of the diagram in the main figure.
- First, the Working Tree and the latest commit are under the same status (Status B in the diagram).
- When you edit files under the Working Tree, the Working Tree status changes from Status B to Status C.
- By running the
git add
command, edited files in the Working Tree will be registered in the staging area(INDEX). At this point, both the Working Tree and the staging area are in Status C. - To make a record of one version using the staged files, run the
git commit
command. The latest commit (HEAD) and the Working Tree become the same status (Status C). - After the commit action, you can continue to edit and move the Working Tree forward to Status D.
- However, you may find an error in the committed files. If that is the case, you can restore the files from the latest commit by running the
git restore
command (the Working Tree will be back to Status C). - If you find another error in a committed file, you can also reverse the committed history by running the
git reset --hard
command. For this purpose, you need to set a commit hash to specify which commit you want to retrieve.- The git reset command can used for clearing the staging area
- Using different options (
--soft
or--mixed
), you can keep the Working Tree and/or the INDEX status.