Skip to main content

Committing with Claude

Module: Git Integration | Lesson: 2 of 4 | Time: ~10 minutes

What You Will Learn

  • How Claude creates Git commits
  • How to review diffs before committing
  • Commit message conventions Claude follows

Prerequisites

Why Let Claude Handle Commits?

In the previous lesson, you learned the basics of Git — what commits are and how to create them. You had to tell Claude exactly what commit message to use. But Claude Code can do much more than that.

When you ask Claude to commit, it:

  1. Checks what changed — runs git diff to see exactly which lines were added, removed, or modified.
  2. Writes a descriptive commit message — based on the actual changes, not a generic "update files."
  3. Stages the right files — only includes the files that should be part of this commit.
  4. Shows you everything before committing, so you can approve or adjust.

This means you get well-documented Git history without having to think about the details.


Asking Claude to Commit

The simplest way is to just ask:

You: Commit my changes

Claude will:

  1. Run git status to see what changed.
  2. Run git diff to examine the specific changes.
  3. Look at recent commit messages to match the project's style.
  4. Propose a commit message.
  5. Stage the appropriate files and create the commit.

You can also be more specific:

You: Commit the changes to index.html and style.css

Or give Claude guidance about the message:

You: Commit these changes — we added a dark mode toggle to the settings page

How Claude Writes Commit Messages

Claude follows established conventions for commit messages. A good commit message that Claude generates looks like:

Add dark mode toggle to settings page

- Add toggle switch component to settings.html
- Add CSS variables for dark/light theme colors
- Add JavaScript to persist theme preference in localStorage

Notice the structure:

  • First line: A short summary (under 70 characters) in the imperative mood ("Add" not "Added").
  • Blank line: Separates the summary from details.
  • Body: Bullet points explaining what specifically changed.
You can ask for changes

If Claude proposes a commit message and you want to adjust it, just say so:

You: Change the commit message to mention that this fixes issue #42

Claude will update the message and recommit.


Reviewing Diffs Before Committing

Before committing, it is good practice to review what actually changed. You can ask Claude to show you:

You: Show me what changed since the last commit

Claude will run git diff and show you the differences. The output looks like this:

--- a/index.html
+++ b/index.html
@@ -10,6 +10,8 @@
<body>
<h1>My Website</h1>
+ <button id="theme-toggle">Toggle Dark Mode</button>
+ <p>Click the button to switch themes.</p>
</body>
</html>

Lines starting with + were added. Lines starting with - were removed. Lines without a prefix are unchanged context.

You can also ask Claude to explain the diff in plain language:

You: Explain what changed in simple terms
Claude: I added two new elements to index.html:
1. A button with the id "theme-toggle" that says "Toggle Dark Mode"
2. A paragraph explaining what the button does
These were added inside the body, after the h1 heading.

Viewing Commit History with Git Log

To see your project's full history, ask Claude:

You: Show me the recent commit history

Claude will run git log and show you something like:

commit b4d5e6f (HEAD -> main)
Author: Your Name <you@example.com>
Date: Wed Apr 1 14:30:00 2026

Add dark mode toggle to settings page

commit a1b2c3d
Author: Your Name <you@example.com>
Date: Wed Apr 1 10:30:00 2026

Initial project setup with HTML, CSS, and JS

You can also ask for more detail:

You: Show me what changed in the last commit

Or look at a specific file's history:

You: Show me the commit history for index.html

Undoing Changes

Everyone makes mistakes. Git and Claude give you several ways to undo things.

Undo Uncommitted Changes (Discard Edits)

If you changed a file but have not committed yet and want to go back:

You: Discard my changes to index.html and revert it to the last commit
This cannot be undone

Discarding uncommitted changes permanently deletes those changes. Make sure you really want to revert before confirming.

Undo the Last Commit (Keep the Changes)

If you committed but the message was wrong or you want to adjust:

You: Undo the last commit but keep the file changes

Claude will run git reset --soft HEAD~1, which removes the commit but leaves your files as they were. You can then make adjustments and commit again.

Undo the Last Commit (Discard Everything)

If you committed and want to completely go back to the state before:

You: Undo the last commit and discard all those changes
Use with caution

This permanently deletes both the commit and the changes. Claude will ask for confirmation before proceeding.

Revert a Specific Commit (Safe Undo)

If you want to undo a commit that happened a while ago without losing everything after it:

You: Create a new commit that reverses the changes from commit a1b2c3d

This is the safest approach — it creates a new commit that undoes the old one, without rewriting history.


Commit Best Practices

Commit Often

Make small, frequent commits rather than one giant commit at the end of the day. This gives you more restore points and makes the history easier to read.

Good: 5 small commits over an afternoon
- "Add header component"
- "Add navigation links to header"
- "Style header with dark theme"
- "Add mobile responsive menu"
- "Fix header alignment on small screens"

Bad: 1 giant commit at the end
- "Add header with navigation, styling, responsive menu, and fixes"

Each commit should represent one logical change. If you fixed a bug and added a new feature, those should be two separate commits.

You: Commit only the changes to the bug fix in utils.js

Then:

You: Now commit the new feature files

Review Before Committing

Always look at the diff before committing. It takes 10 seconds and catches mistakes like accidentally including debug code, temporary files, or incomplete changes.

You: Show me what will be committed, then commit if it looks good

Try It Yourself

Continue with the git-practice project from the previous lesson (or create a new one):

  1. Ask Claude to create a new file:

    Create a file called calculator.js with functions for add, subtract,
    multiply, and divide
  2. Ask Claude to commit without specifying a message:

    Commit my changes

    Notice the commit message Claude generates — it should describe the actual changes.

  3. Make another change:

    Add an exponent function to calculator.js
  4. Review the diff before committing:

    Show me what changed since the last commit
  5. Commit with your own guidance:

    Commit this change — we added the exponent function
  6. View the history:

    Show me the last 3 commits with details
  7. Practice undoing — make a change and then undo it:

    Add a comment that says "TODO: delete this" to calculator.js

    Then:

    Discard my uncommitted changes to calculator.js

What You Learned

  • Claude can automatically write commit messages based on the actual changes in your code.
  • Always review the diff before committing to catch mistakes.
  • git log shows your complete commit history — ask Claude to show it any time.
  • You can undo uncommitted changes (discard edits), undo commits (soft or hard reset), or revert specific commits (safe undo).
  • Best practices: commit often, commit related changes together, and review before committing.

Help Us Improve

How was this lesson? Take 2 minutes to share your feedback — it helps us make the tutorials better for everyone.

Give Feedback →

Next Up

Next: Pull Requests and Reviews