Edit and Commit Overview (2)

Preparing for Git Branches in Edit and Commit

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.svg Working Tree or Working Directory, which you see on your local computer for daily editing.

staging-area.svg 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.svg 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.

  1. First, the Working Tree and the latest commit are under the same status (Status B in the diagram).
  2. When you edit files under the Working Tree, the Working Tree status changes from Status B to Status C.
  3. 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.
  4. 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).
  5. After the commit action, you can continue to edit and move the Working Tree forward to Status D.
  6. 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).
  7. 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.