Commit, Branch and Open PRs with Claude Code
- The official pull request workflow is three prompts: summarize your changes, then “create a pr”, then ask Claude to enhance the PR description. The docs add one rule: review Claude’s generated PR before submitting, and ask Claude to highlight risks.
- Claude Code links the session to the PR when it creates one with
gh pr create(orglab mr create). Later,claude --from-pr 1234(your own number) reopens the sessions linked to that PR. - For parallel work on separate branches,
claude --worktree feature-authgives each session its own checkout on its own branch. The repository needs at least one commit first. - Undo in two layers.
/rewind(orEsctwice on an empty prompt) rolls back files Claude edited with its own tools, but the checkpointing page says changes made only by Bash commands are not tracked, and tells you to use Git for those and for permanent history. - The best-practices page shows a rule that asks before every push:
"ask": ["Bash(git push *)"]in.claude/settings.json. - We read three official pages on 2026-11-02. We did not run Claude Code for this tutorial. The Git commands are standard Git; the prompts and the commit-message advice are our own.
What you’ll be able to do after this tutorial
Section titled “What you’ll be able to do after this tutorial”- Ask Claude to look at your changes and draft a clear commit message, then commit only after you have read it.
- Work on a separate branch so the main line stays safe.
- Ask for a pull request description that says what changed, why and how it was tested.
- Undo a bad change with
/rewindor Git, and check the diff before anything is pushed.
Concepts to know first
Section titled “Concepts to know first”A commit is a saved snapshot of your changes with a message. A branch is a separate line of work, so experiments do not touch main. A pull request (PR) is a request to merge your branch into another one, with a page where others can review it. A diff is the list of changed lines. 1. Getting started with Claude Code shows how to start. Coding 13 covers allow, ask and deny rules. Coding 15 shows how to have a change reviewed. For GitHub work in the cloud, see Coding 3, and for checks that run automatically, Coding 10.
Step-by-step guide
Section titled “Step-by-step guide”-
Start on a branch
Create a branch for the task before you ask Claude to edit anything. In your own terminal:
Terminal window git switch -c fix-login-redirectgit statusBranch names are your choice; a short description is easiest to find later. To run two tasks at once, the common-workflows page offers worktrees:
claude --worktree feature-authstarts a session in an isolated checkout on its own branch, and a second terminal with a different name starts another one. The repository needs at least one commit first; in a repository with no commits the command fails withFailed to resolve base branch "HEAD". The page links to a Worktrees page for cleanup, which we did not read. -
Set a safety rule for pushes (optional)
The best-practices page shows a permissions example where
Bash(git push *)is in theasklist, so Claude asks before every push. It also showsdenyentries likeRead(./.env)to keep secret files out of Claude’s reach. Put rules in.claude/settings.json, as explained in Coding 13:{"permissions": {"ask": ["Bash(git push *)"],"deny": ["Read(./.env)", "Read(./secrets/**)"]}}Rules match tool calls, not intent, so treat them as one layer of protection and read the page for the limits.
-
Make a small change, then look at it yourself
Ask Claude for one small, clear change, using a prompt like our example below. Then, before any commit, check what changed in your own terminal:
Terminal window git status # which files changed or are newgit diff # every changed lineLook for files you did not expect, such as logs, build output or
.env. -
Ask for a commit message, then commit
Claude can read the diff and draft the message. We prefer to review it first:
Copy and paste this promptLook at my current changes with git diff. Draft a commit message: a first line of at most 50 characters in the imperative mood (for example “Fix login redirect”), then a short body saying why the change was needed. Do not commit yet. Show me the message and list any file that looks unrelated.
If the message is accurate, either commit it yourself (
git add <files>thengit commit -m "...") or tell Claude “commit only these files with that message”. The 50-character line and imperative mood are common Git conventions, not rules from the Claude Code docs. Stage specific files rather than everything, so unrelated changes do not slip in. -
Keep commits small and separate
One commit per idea makes history readable and makes undoing easy. If your changes mix two ideas, ask:
Copy and paste this promptMy working tree has two kinds of changes: a bug fix and a formatting cleanup. Suggest how to split them into two commits, which files belong in each, and a message for each. Do not run any git command until I agree.
-
Write the pull request
The official page says you can ask directly (“create a pr for my changes”) or go step by step: summarize your changes, run “create a pr”, then refine, for example “enhance the PR description with more context about the security improvements”. Creating a PR needs a remote and, in our understanding, the
ghtool signed in; the best-practices page says that withghinstalled and signed in, Claude will open pull requests and read review comments without explanation. We have not confirmed other setups. A prompt to use first:Copy and paste this promptSummarize the changes on this branch compared with main in plain words. Then draft a PR description with these sections: what changed, why, how I tested it, risks, and anything reviewers should check. Show it to me and do not open the PR yet. If something was not tested, say so instead of claiming it was.
When the description is right, ask Claude to open the PR (or run
gh pr createyourself). The page says to review the generated PR before submitting and ask Claude to highlight potential risks or considerations. -
Push on purpose, then review
Pushing sends your branch to the remote. With the
askrule from step 2, Claude has to ask first; if you are not sure, push yourself withgit push -u origin <branch>. After the PR exists, run/code-reviewfor a review by a fresh subagent that did not see your conversation (see Coding 15), then read the PR page as your reviewers will. -
Undo what went wrong
Pick the layer that matches the damage:
Situation What to use Claude drifted in this session Escto stop; it keeps everything up to that pointClaude’s file edits in this session were wrong /rewind, orEsctwice on an empty prompt, to roll back code, conversation or bothA file was changed only by a Bash command /rewinddoes not track it; use Git (git restore <file>)Last commit message or content is wrong, not pushed git commit --amend(standard Git)A commit is already pushed or shared Use git revert(standard Git) to add a new undoing commit rather than rewriting shared historyThe checkpointing page says checkpoints are for quick session-level recovery and that you should keep using Git for commits, branches and long-term history. Rewinding does not replace your commits.
Quick reference
Section titled “Quick reference”| Task | Command or place |
|---|---|
| New branch | git switch -c <name> |
| Isolated parallel session | claude --worktree <name> |
| See what changed | git status, git diff |
| Ask before pushing | "ask": ["Bash(git push *)"] in .claude/settings.json |
| Create a PR | ask “create a pr” (Claude uses gh pr create) |
| Reopen a PR’s session | claude --from-pr 1234 (your own number) |
| Roll back Claude’s edits | /rewind or Esc twice on an empty prompt |
| Review before merge | /code-review |
Common problems
Section titled “Common problems”The commit contains files I did not mean to include
Section titled “The commit contains files I did not mean to include”- Cause: Everything was staged at once.
- Fix: Run
git statusbefore committing and stage named files. To remove one from staging, usegit restore --staged <file>.
The commit message says more than the change does
Section titled “The commit message says more than the change does”- Cause: The message was drafted from the goal, not the actual diff.
- Fix: Ask Claude to base the message only on
git diff, and edit it yourself.
Claude could not create the PR
Section titled “Claude could not create the PR”- Cause: No remote, no signed-in
gh, or no commits pushed yet. We have not confirmed the exact error messages. - Fix: Check
git remote -vandgh auth statusyourself, push the branch, then ask again.
/rewind did not undo a change
Section titled “/rewind did not undo a change”- Cause: The file was changed only by a Bash command, which checkpoints do not track.
- Fix: Use Git:
git restore <file>orgit switchback to a clean branch.
Practical tips
Section titled “Practical tips”- Practice in a throwaway repository first, then move to real work.
- Commit before you start a big Claude task, so you have a clean point to return to.
- Read every diff yourself. A commit message that sounds right can still describe the wrong change.
- Never commit secrets. If one was committed or pushed, treat it as exposed and rotate it.
- Re-check commands against the official pages, since Claude Code changes often.
Next tutorials
Section titled “Next tutorials”Sources
Last verified: Nov 16, 2026
- Common workflows (Create pull requests, Run parallel sessions with worktrees) code.claude.com
- Best practices for Claude Code code.claude.com
- Checkpointing code.claude.com
Prices, limits, and menu names change often. Before making an important decision, check the official pages above for the latest details.
Finished this tutorial?
Progress is saved only in this browser and never sent anywhere.