Git manages files across three main environments

  1. Working Directory: This is where you directly interact with your project files. Files here reflect their state since the last commit. Changes made here are not yet tracked by Git.

  2. Staging Area (Index): A temporary holding area for changes you’ve made but haven’t yet committed. Use git add to move changed files from the working directory to the staging area. Think of it as preparing changes for the next commit. (consider it as dating before marrying xD)

  3. Commit: A snapshot of your project at a specific point in time. Each commit is uniquely identified by a hash (a long string of characters). Commits are permanent records of your project’s history. Use git commit -m "message" to create a new commit from the staged changes.

File States

Files in the working directory can exist in several states:

  • Untracked: New files not yet known to Git. They are not part of the project’s history.
  • Tracked: Files known to Git (they existed in a previous commit). These files can be further categorized:
    • Unmodified: The file hasn’t been changed since the last commit.
    • Modified: The file has been changed since the last commit.
    • Staged: The modified file has been added to the staging area, ready to be included in the next commit.

Git Commands for Managing File States:

  • git status: Shows the current status of your files (tracked/untracked, modified/unmodified, staged/unstaged).
╰─❯ git status                                                                                                                                              
On branch v4
Your branch is up to date with 'origin/v4'.
 
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   quartz.layout.ts
 
no changes added to commit (use "git add" and/or "git commit -a")
  • git add <file> or git add .: Stages changes (moves modified files to staging area).
  • git commit -m "message": Creates a new commit from staged changes.
  • git restore <file> or git restore .: Discards changes in the working directory and reverts to the last committed version.
  • git restore --staged <file> or git restore --staged .: Unstages changes (removes files from the staging area), leaving them modified in the working directory.
  • git checkout .: (Older command; equivalent to git restore . )

Key Concept: Branches

Git uses branches to organize different versions of your project. The HEAD pointer always indicates the currently active branch. By default, a new repository starts with a main (or master) branch. Branches allow you to work on new features or bug fixes in isolation without impacting the main project.

Understanding these environments and file states is critical for effectively using Git. git status is your friend! Use it frequently to see where your files are and what changes are pending.