Chapter 4: The Basic Workflow
Now that you understand the concepts, let's look at the basic cycle of making changes and saving them in Git.
1. Initializing a Repository: git init
To start tracking a project with Git, navigate to your project's root folder in the terminal and run:
git init
This creates the .git directory and sets up everything Git needs to track your files.
2. Checking the Status: git status
The git status command is your best friend. It tells you exactly what is happening in your repository:
- Which files have been changed?
- Which files are being tracked?
- What is in the staging area?
- What branch are you on?
Run it often!
git status
3. Staging Changes: git add
In Git, you don't just commit files; you "stage" them first. This allows you to choose exactly which changes should go into your next commit.
- To stage a specific file:
git add filename.txt - To stage all changes:
git add . - To stage a whole directory:
git add src/
4. Saving a Snapshot: git commit
Once your changes are in the staging area, you can commit them to the repository's history. Every commit must have a message explaining what you changed.
git commit -m "Add initial login functionality"
Writing Good Commit Messages
A good commit message should be:
- Short and descriptive (under 50 characters).
- Written in the imperative mood ("Add feature" instead of "Added feature").
- Clear about the "why", not just the "what".
5. Avoiding Tracking: .gitignore
Often, there are files you don't want Git to track, such as:
- Log files
- Temporary build files (e.g.,
node_modules/,target/) - IDE settings (e.g.,
.vscode/,.idea/) - Secrets and API keys
You can create a file named .gitignore in your project root and list the patterns you want Git to ignore:
# Ignore node_modules
node_modules/
# Ignore log files
*.log
# Ignore OS files
.DS_Store
Thumbs.db
Summary of the Workflow
- Modify files in your working directory.
- Stage the changes you want to include in your next snapshot:
git add <file>. - Commit the staged changes:
git commit -m "message". - Repeat.
In the next chapter, we'll learn how to look back at the history we've created.