How to Use Git With Claude Code
Claude Code can work with files and run commands in your project, which makes Git review especially important: a request may change several files before you look at the result. Use this workflow to preserve your starting work, keep a task isolated, inspect Claude's edits and commands, and decide exactly what to commit.
Practice Git workflows →Start Claude Code from the right repository
Open a terminal at the root of the project you intend to change. Confirm the directory, branch, and current changes before starting a Claude Code session:
pwd
git rev-parse --show-toplevel
git branch --show-current
git status --shortIf git status shows existing edits, decide how to preserve and distinguish them before the assistant starts. Commit changes that are ready, or leave them in place and make a note of what was already modified. Do not assume an assistant-created checkpoint will include or protect every uncommitted file.
Isolate the task on a branch
For work you want to review separately, create a branch from the correct starting branch:
git switch -c fix/handle-empty-searchIf the working tree already has changes, switching branches may carry them along when Git permits it. A branch is not a substitute for saving or understanding unfinished edits. Before a larger task, consider making a reviewed commit that represents the starting point.
Give Claude Code a bounded task and review commands
State the expected outcome, constraints, and verification steps. For example, ask it to fix a specific bug, preserve unrelated behavior, identify the files it touched, and report the tests it ran. A clear scope makes both the code and the Git diff easier to evaluate.
Review each shell command before allowing it to run, according to your project and Claude Code's permission configuration. Commands that delete files, discard changes, rewrite commit history, install software, or publish code deserve extra scrutiny. Never paste credentials into a prompt or approve a command that exposes them.
Do not ask the assistant to both implement a feature and push it without first reviewing the resulting diff. Keeping code review, tests, commits, and remote publishing as explicit checkpoints makes mistakes easier to catch before they affect collaborators.
Inspect Claude Code changes with Git
When the task is done, check the entire working tree, including files that have not been staged:
git status --short
git diff --stat
git diff
git diff --cached- Confirm that the changed files belong to the requested task.
- Read the diff for logic errors, unrelated edits, or deleted work.
- Check untracked files; they are not included in the ordinary diff until staged.
- Look for hard-coded secrets, local configuration, debug output, and generated files that should not be committed.
Test, stage, and commit intentionally
Run the checks appropriate to the repository and inspect their results. Then stage only the intended files and review the exact snapshot that will be committed:
git add path/to/changed-file
git diff --cached
git commit -m "Handle empty search queries"Replace the example path and message with values that describe your change. For mixed edits in one file, use git add -p to stage selected hunks, then recheck git diff --cached. Push only after verifying the branch and remote and deciding the change is ready to share.
Recover from an unwanted edit
Stop and inspect the current changes before using a recovery command. For selective unstaged changes, git restore -p -- path/to/file lets you choose hunks. The command git restore -- path/to/file discards all unstaged changes in that file, including your own edits, so use it only after reviewing what would be lost.
If a bad commit has already been shared, git revert usually creates a safer corrective commit than rewriting shared history. For a broader recovery checklist, read how to undo AI-generated changes safely.
Claude Code and Git FAQ
Does Claude Code automatically protect uncommitted changes?
Do not rely on an assistant to preserve work you have not checked. Review git status before and after the task, and save important starting changes in a way you understand.
Should I let Claude Code run Git commands?
Only approve commands you understand and intend to run. Inspect commands that discard files, rewrite history, or push changes especially carefully; keep review and publishing under your control.