Blog
4 min read

Using Git With Claude Code: Commits, Branches, PRs and Undo

Claude Code can run Git for you — commit, branch, write messages, open pull requests and resolve conflicts. A simple, safe workflow for beginners: branch first, commit after every working step, review the diff, and how to undo when it goes wrong.

Claude Code can use Git directly — it runs the same git commands you would. That makes Git far less intimidating for beginners, and it's also your best safety net when an AI is changing your code.

Why Git matters more with an AI

An AI coding tool can change twenty files in a minute. Git gives you:

  • A record of exactly what changed
  • Save points you can go back to
  • Branches so experiments don't touch working code

If you're brand new, skim Git and GitHub for beginners first.

The workflow

1. Start on a branch

Before a new piece of work, ask:

Create a new branch called add-password-reset.

Claude runs git checkout -b add-password-reset. Your main branch stays untouched until you choose to merge. (Git branches explained)

2. Commit after every working step

This is the habit that matters most. When something works — tests pass, the page loads — commit it:

The form works now. Commit this.

Claude looks at the changes and writes a descriptive commit message. Small, frequent commits mean that if the next change breaks things, you lose minutes, not hours. (Good commit messages)

You can put this in your CLAUDE.md so you don't have to ask:

# Git
- Work on a branch, never directly on main
- Commit after each working step with a clear message
- Run the tests before committing

3. Review what changed

Before you commit or merge, look at the diff:

Show me what you changed in this session.

Or type /diff. Reading the diff is how you catch the change you didn't ask for — a deleted test, a hardcoded key, a file it "tidied up". (How to read a diff)

4. Open a pull request

If your project is on GitHub and you have the gh command-line tool installed and logged in:

Push this branch and open a pull request describing the changes.

Claude pushes, then creates the PR with a summary. A PR is a good place to pause and review even if you work alone. (What is a pull request?)

5. Merge

Once you're happy, merge the PR on GitHub (or ask Claude to merge the branch locally), then switch back to main and pull.

Things Claude handles well

  • Writing commit messages from the actual diff
  • Resolving merge conflicts — ask it to explain both sides before choosing (Merge conflicts explained)
  • Reading history — "when did this function change and why?" (it can read git log and git blame)
  • Untangling errors like rejected pushes or a detached HEAD (git push rejected)

Undoing things

Two different undo systems exist, and it helps to know both:

  • Claude Code's checkpoints undo Claude's own edits within a session. Press Esc twice or run /rewind. (Checkpoints and rewind)
  • Git undoes anything that's been committed, across sessions. "Undo the last commit but keep the changes" → git reset --soft HEAD~1. (How to undo almost anything in Git)

Checkpoints don't track changes made by shell commands (like a database migration or rm), and they live only in that session. Git commits are the durable safety net.

Rules worth setting

Some Git actions are hard to reverse. Ask Claude to check with you before:

  • git push --force
  • git reset --hard
  • deleting branches
  • pushing to main

You can enforce this with permission rules in settings.json so these commands always ask first:

{
  "permissions": {
    "ask": ["Bash(git push *)", "Bash(git reset --hard *)"]
  }
}

(Claude Code settings explained)

Running several tasks at once

Want Claude working on two features without them colliding? Git worktrees give each task its own folder on its own branch. (Parallel Claude Code agents with worktrees)

The summary

  • Branch before starting; commit after every working step.
  • Read the diff before committing or merging.
  • Checkpoints undo within a session; Git undoes across sessions.
  • Make force-push and hard reset ask first.

EasySpawn gives Claude Code a persistent server with your repository, Git history and running app all in one place — commits and branches are still there when you come back tomorrow. See how it works or join the waitlist.

Related: Git and GitHub for Beginners · How to Undo Almost Anything in Git · Claude Code GitHub Actions · How to Review a Pull Request Written by an AI Agent

Keep reading