How to commit changes in Git
· Category: Git
Short answer
Run git commit -m "descriptive message" to save staged changes as a new snapshot in the repository history.
Steps
- Stage your changes:
git add .
- Commit with a message:
git commit -m "Add user authentication module"
- Commit all tracked files directly (skip staging):
git commit -am "Fix login bug"
- Amend the last commit:
git commit --amend -m "Updated message"
Tips
- Write commit messages in the imperative mood (e.g., "Add feature" not "Added feature").
- Keep the first line under 50 characters for readability.
- Use the body to explain what and why, not how.
Common issues
- Forgetting to stage files before committing results in an empty commit.
- Pushing amended commits that have already been shared can confuse collaborators.