~/academy $ claude learn git-with-claude-code
Git with Claude Code
Branches, commits, reviewing diffs, writing commit messages and pull requests with Claude Code, and undoing work with /rewind and git. 8 min
Watch it done: branch, review, commit and open a pull request
Step 1 of 7
Ask for a branch before the work starts, never after. Plain words are enough.
- Ask for a branch before the work starts, never after. Plain words are enough. Type: create a new branch called fix/due-date-sort. Claude runs the git commands to create the branch.
- Once Claude has made the fix, ask what changed before you do anything else. Type: what files have I changed?. Claude checks git and lists the changed files.
- Read the actual changes before you commit them. Type: /diff. A view of your working tree opens, listing each changed file with its added and removed line counts, including the edits Claude has made so far.
- Prefer git itself? Start a line with ! to run a shell command directly from the prompt. Type: ! git status. The command runs straight away without Claude needing to approve it. Its output lands in the conversation and Claude responds to it.
- Commit after each step that works. Small commits are easy to read and easy to undo. Type: commit my changes with a descriptive message. Claude writes the message and commits. By default the commit carries a Co-Authored-By line, which the attribution setting can change or hide.
- Share the change. Read the description Claude wrote before anyone else does, because it goes out under your name. Type: create a pr. With the GitHub CLI, gh, installed and signed in, Claude opens the pull request with it.
- Undo a bad step from this session. For anything older, or changed by a script, use git. Type: /rewind. The rewind menu lists the prompts you sent. Pick one, then restore the code, the conversation, or both. Files changed only by shell commands are not restored.
Commands from the official Claude Code docs, checked 18 November 2026. Lines marked Note describe what happens and are not real output.
$ Read
It already knows your repo
When a conversation starts in a git repository, Claude Code gives Claude a snapshot: the current branch, the main branch, git status and recent commits. It also has built-in instructions for writing commits and pull requests. So you ask in plain words.
For GitHub, install the GitHub CLI, gh, and sign in. Claude then opens pull requests and reads review comments with it, without any explanation from you.
Commits Claude makes carry a Co-Authored-By line by default, and pull requests get a short attribution line. You can change or hide both with the attribution setting.
create a branch called fix/login-timeout and switch to it
commit my changes with a descriptive message
create a pr for this fix
Lesson notes: Git with Claude Code
Everything in this lesson as text, for revision or to read without the interactive steps. By the end you will be able to: You can work on a branch, review and commit Claude's changes, open a pull request and roll back a bad step.
Step 1 of 6 · Read
It already knows your repo
When a conversation starts in a git repository, Claude Code gives Claude a snapshot: the current branch, the main branch,
git statusand recent commits. It also has built-in instructions for writing commits and pull requests. So you ask in plain words.For GitHub, install the GitHub CLI,
gh, and sign in. Claude then opens pull requests and reads review comments with it, without any explanation from you.Commits Claude makes carry a
Co-Authored-Byline by default, and pull requests get a short attribution line. You can change or hide both with theattributionsetting.Start safely create a branch called fix/login-timeout and switch to it
Save a step commit my changes with a descriptive message
Share it create a pr for this fix
Step 2 of 6 · Read
Branch first, commit often
Ask for a branch before the work starts, never after. Then commit after each step that works: one commit for the failing test, one for the fix, one for the tidy up. Small commits are easy to read and easy to undo.
Good commit messages say why, not just what. Ask for that: "commit with a message that explains the cause of the bug, not just the files changed".
For two jobs at once,
claude --worktree feature-authstarts a session in its own separate checkout on its own branch, so the two sessions' edits never collide.Step 3 of 6 · Try it in the terminal
Look before you commit
Claude says the fix is done. Before you commit, open the view of everything changed in your working tree, including Claude's edits so far.
Hint: A four letter slash command, the same word git uses.
Show the command
$ /diff Changes (Current) src/auth/session.ts +12 -3 tests/auth/session.test.ts +28 -0 - if (expiresAt < now) refresh() + if (expiresAt - now < REFRESH_MARGIN_MS) refresh()
Step 4 of 6 · Read
Reviewing the change
/diffshows every uncommitted change. Prefer git itself? Type! git statusand the output lands in the conversation without Claude needing to run anything./code-review(alias/review) reviews the current diff for correctness bugs in a fresh helper that never saw your conversation, so it is not swayed by the reasoning that produced the code./security-reviewchecks your branch against the remote default branch for issues such as injection and data exposure.Before opening a pull request, ask Claude to summarise the change and point out risks. Then read the description it wrote. It is going to your colleagues under your name.
Step 5 of 6 · Read
Undo: /rewind for the session, git for everything else
Claude Code saves a checkpoint before each prompt you send. Run
/rewind(also/undo), or pressEsctwice with an empty prompt, and pick a point. You can restore the code, the conversation, or both.Checkpoints only track edits made with Claude's own file editing tools in your main conversation. A file changed only by a shell command, or by a helper subagent, is not restored. They are a quick session undo, not a replacement for git.
If you committed after each working step, getting back to safety is one instruction: "revert the last commit" or "discard my uncommitted changes to session.ts". Claude runs the git commands and you check the result with
/diff.Step 6 of 6 · Quiz
Check your understanding
Claude ran a formatting script that rewrote four files. You run /rewind and those files do not change back. Why?
- Checkpoints expire after five minutes.
- Checkpoints track edits from Claude's file editing tools, and a file only a shell command changed is not tracked. Use git to restore it.
- You need to be in plan mode to rewind.
Show the answer
Checkpoints track edits from Claude's file editing tools, and a file only a shell command changed is not tracked. Use git to restore it. Right. That is why committing before a big step matters: git covers what checkpoints do not.