The Save Cycle (add and commit)
Unlike pressing Ctrl + S in Word, saving changes officially in Git is a deliberate two-step process. This is the essence of working with Git.
There are three main "areas" in a local Git repository:
- Working Directory: Your real files on your computer. When you modify them, they stay here.
- Staging Area: A "waiting room". Here you put the modified files that you are almost certain you want to save in the next official version.
- Repository (Commit History): The permanent official history. Once something gets here, Git protects you from losing it.
1. Check what changed: git status
Always start by looking at what you have modified.
git status
Modified or new files will appear in red. This means that Git has noticed the changes, but they are in the Working Directory; they will not be included in the next save if you do nothing.
2. Prepare changes: git add
To move your changes to the "waiting room" (Staging Area), you use the add command. You can add specific files or everything at once.
# To add a single file
git add index.html
# To add ALL changes in the current folder (The most used!)
git add .
If you now run git status, you will see those files in green. They are in the staging area, ready to be officially saved.
3. Officially save: git commit
A commit is like taking a permanent "photo" of the current state of your project (specifically of what was in the green Staging Area).
Golden Rule: Every commit mandatorily needs a message describing what was done.
git commit -m "Add initial styling for the navigation menu"
The -m stands for "message". The message must be in quotes, short and preferably in present or imperative tense (e.g., "Fix bug" instead of "Fixed a bug that existed").
Daily Workflow Summary
80% of your time using Git will follow this repetitive cycle:
- Write code...
git status(You see the code in red)git add .(You put the changes in the waiting room)git status(You verify it's green and ready)git commit -m "Clear description"(You save the version officially)- Repeat.