Staging and committing are essential steps in Git's version control process. These actions allow you to prepare changes and save snapshots of your project's state.
The staging area is an intermediate space where you prepare changes before committing them to the repository. It allows you to:
- Review changes.
- Group related modifications into a single commit.
- Modify files in your working directory.
- Use
git add
to stage the changes. - Commit the staged changes to save them in the repository.
A commit represents a snapshot of your project at a specific point in time. It includes:
- All staged changes.
- A commit message describing the changes.
To stage a single file:
git add filename
To stage all modified and new files:
git add .
To stage files matching a pattern:
git add *.txt
To remove a file from the staging area without discarding changes:
git reset filename
To commit all staged changes:
git commit -m "Your commit message"
For multiline messages, use:
git commit
This opens your default text editor for writing a detailed description.
To modify the most recent commit (e.g., update its message or add more changes):
git commit --amend
To view the history of commits:
git log
- View a summary of commits:
git log --oneline
- View changes made in each commit:
git log -p
Create or modify a file:
echo "Git is awesome!" > example.txt
Check the current state of your repository:
git status
Add the file to the staging area:
git add example.txt
Commit the file with a message:
git commit -m "Add example.txt with a message"
View the commit history:
git log --oneline
- Write Clear Commit Messages: Describe what and why changes were made.
- Commit Often: Make small, incremental commits to simplify debugging and history tracking.
- Group Related Changes: Stage and commit logically related changes together.
Command | Description |
---|---|
git add filename |
Stage a specific file. |
git add . |
Stage all changes. |
git commit -m "message" |
Commit staged changes with a message. |
git reset filename |
Unstage a file while keeping changes. |
git commit --amend |
Modify the last commit. |
git log |
View commit history. |
git log --oneline |
View a concise summary of commit history. |
Staging and committing are at the heart of Git workflows. By mastering these steps, you can effectively organize your project changes and maintain a clean, descriptive history.
Next Steps: Branch Basics