Git manages files across three main environments
-
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.
-
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) -
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 add <file>
orgit add .
: Stages changes (moves modified files to staging area).git commit -m "message"
: Creates a new commit from staged changes.git restore <file>
orgit restore .
: Discards changes in the working directory and reverts to the last committed version.git restore --staged <file>
orgit restore --staged .
: Unstages changes (removes files from the staging area), leaving them modified in the working directory.git checkout .
: (Older command; equivalent togit 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.