How to Use Git With Cursor, Claude Code, or Copilot Without Losing Your Code

Cursor, Claude Code, and GitHub Copilot can help write and edit code, but their changes still affect your working files. The safest approach is to let Git track what changed, review each diff, and keep a clear recovery point before accepting a larger edit. Use this workflow whether your coding assistant suggests changes or applies them directly.

Practice Git workflows →

Before you start: check what already changed

Start from a known state. Check your current branch and working tree before asking a tool to edit files. If you already have unfinished changes, commit them if they are ready, or save them separately so they are not confused with the assistant's work.

git branch --show-current
git status --short

For a focused task, create a branch before you begin. It gives the work a separate line of history and makes it easier to review or abandon without changing the branch you started from:

git switch -c ai/add-search-filter

Make one small change at a time

Give your coding assistant a specific task and review its changes before moving on to another request. A small, clear diff is easier to understand, test, and undo than a large collection of unrelated edits. After each task, inspect the affected files and run the project's relevant checks.

git status --short
git diff --stat
git diff

If you staged files, also use git diff --cached. Check git status for new, untracked files because they do not appear in the ordinary diff until staged. Review each change for correctness, scope, and unexpected edits before accepting it.

Create checkpoints you can understand

Once a coherent part of the task is reviewed and tested, commit it with a message that explains the change. Small, meaningful commits make it easier to see how the work evolved and to reverse a specific step later.

git add -p
git diff --cached
git commit -m "Add search filter to results"

git add -p lets you select individual changes for staging. Review the staged diff before committing, especially when your own edits and assistant-generated edits are mixed in the same file. Do not commit credentials, local settings, or other files that should remain private.

Avoid overlapping edits from multiple AI tools

Two assistants or terminal sessions working in the same checkout can edit the same file at once. Git does not automatically know which edit you intended, and one tool may overwrite another tool's unsaved changes. Prefer one active editor per working tree, and check git status and the diff before starting another task.

If you need independent parallel work, Git worktrees provide separate directories attached to separate branches:

git worktree add ../project-ai-task -b ai/another-task

The new directory is a separate working tree in the same repository. Keep each tool on its assigned branch and review any commits before merging them. Do not point multiple tools at the same worktree and assume Git will protect unsaved edits.

If an AI edit is wrong, undo it selectively

For a mixed diff, use git restore -p -- path/to/file to choose which unstaged hunks to discard. To discard all unstaged changes in one file, first inspect git diff -- path/to/file, then use git restore -- path/to/file only if you intend to lose every unstaged edit in that file. Untracked files require separate review; avoid broad cleanup commands that might delete unrelated work.

If the unwanted change is already in a shared commit, use git revert <commit-id> to create a new commit that reverses it. This preserves the shared history. For a detailed recovery walkthrough, see how to undo AI-generated changes safely.

A reusable Git checklist for Cursor, Claude Code, and Copilot

  1. Check the branch and existing changes before starting.
  2. Use a task branch for focused work.
  3. Ask for one bounded change and inspect the complete diff.
  4. Check untracked files and staged changes as well as tracked edits.
  5. Run relevant tests before keeping the change.
  6. Commit reviewed work in small, understandable checkpoints.

Git with AI coding tools FAQ

Does Cursor, Claude Code, or Copilot replace Git?

No. These tools can help produce or modify code, but Git tracks versions, branches, and commits. You remain responsible for reviewing the changes and deciding what to keep.

Should I let an AI tool run Git commands automatically?

Understand commands before approving them, especially commands that overwrite files, discard changes, rewrite commit history, or push to a remote. Review the current branch and working tree before any destructive or shared-repository operation.

Learn Git Faster by Practicing

Reading is just the first step. Real learning happens when you practice.

Start Learning Git →