Committing is the moment you make your changes official. This is where you create a snapshot of your project at a specific point in time. Good commit messages are important โ they help you and your team understand what changed and why.
Creating Commits
Always write clear commit messages. If you're fixing a bug, mention the issue. For large features, start with a short summary and add more details if needed. You can always amend a commit with git commit --amend to change the message without creating a new commit.
$ git commit -m "Fix login bug - handle invalid credentials"
$ git commit --amend -m "Updated commit message"
$ git log --oneline
Writing Good Commit Messages
A well-written commit message makes your project history valuable. Follow this format for consistency:
$ git commit -m "Add user registration endpoint
- Create POST /api/register route
- Add input validation for email and password
- Hash passwords with bcrypt before storing
- Return appropriate error messages for duplicate emails"
The first line is a summary under 50 characters. Leave a blank line, then add a detailed description of what changed and why. This format works well with tools like git log and GitHub's commit viewer.
The -a Flag
The -a flag automatically stages all modified tracked files before committing. This is convenient for small fixes but be careful with it โ you might accidentally commit files you didn't intend to include:
# Stage and commit all tracked changes
$ git commit -am "Update header styling"
# This is equivalent to:
$ git add .
$ git commit -m "Update header styling"
Use -a only when you're sure all changes in the working directory should be committed. For new files that aren't yet tracked, you still need to use git add first.
Viewing Commit History
Git keeps a detailed history of every commit. Use these commands to browse it:
# Compact one-line view
$ git log --oneline
# See who made each change
$ git log --author="Alice"
# Show changes in each commit
$ git log -p
# Graph view showing branches
$ git log --oneline --graph --all
The --graph flag is especially useful when working with branches, as it shows a visual representation of how branches diverge and merge.