If you build software with an AI coding agent, Git can feel like a trap. You ask the agent to add a feature, it edits twelve files, and suddenly you are afraid to touch anything in case you break what already worked.
Git is not a test of memory. It is a safety net built around a five-step loop: create a branch, save snapshots, open a pull request, merge to main, and sync your local copy. You can let an agent run the commands for you, as long as you know what to ask for and how to check what happened.
TL;DR
Never build new features directly on main. Use a prompt-first workflow: ask your coding agent to handle each Git step, then run one short check (git status, git log -n 3 --oneline, or gh pr status) to verify it. After a pull request merges, switch back to main, pull the latest code, delete the old branch, and start fresh. If things break locally before you push, use git restore, git switch -c, or git reset --soft HEAD~1 to recover without losing your mind.
The Five-Step Loop in One Picture
Every project—whether you write every line or prompt an agent—follows the same loop. main stays clean and deployable while each change lives on its own temporary branch.
flowchart LR
A["1. Sync main locally"] --> B["2. Create a feature branch"]
B --> C["3. Edit and commit snapshots"]
C --> D["4. Push and open a Pull Request"]
D --> E["5. Review, check, and merge to main"]
E --> A
Two concepts explain 90% of Git:
- Local vs. remote: Your laptop holds your local repository. GitHub holds the remote copy (
origin). Changes stay on your laptop until youpush, and changes on GitHub stay off your laptop until youpull. - Commit vs. branch: A commit is a saved snapshot of your files with a short message. A branch is a separate lane of commits so you can experiment without touching
main.
Step 1: Create a Repository and Start From Zero
You have two starting points: a brand-new project on GitHub that you want on your laptop, or a folder on your laptop that you want to put on GitHub.
Prompt for your agent
“Initialize a new private GitHub repository named
my-first-appwith aREADME.md, clone it into my current folder, and show megit statusso I can confirm we are onmain.”
Commands underneath
If you are starting from scratch with the GitHub CLI (gh):
gh repo create my-first-app --private --add-readme --clone
cd my-first-app
If you already have a local folder with files and want to push it to a new GitHub repo:
git init -b main
git add .
git commit -m "Initial commit"
gh repo create my-first-app --private --source=. --remote=origin --push
How to verify
Run git status and git remote -v. You want to see On branch main and origin pointing to your GitHub URL.
Step 2: Clone an Existing Repository
When a project already lives on GitHub, cloning downloads the files and the full commit history to your machine.
Prompt for your agent
“Clone
https://github.com/<your-user>/<repo-name>into my projects folder, check which branch we are on, and pull the latest changes.”
Commands underneath
git clone https://github.com/<your-user>/<repo-name>.git
cd <repo-name>
git status
Or with GitHub CLI (shorter, no full URL needed):
gh repo clone <your-user>/<repo-name>
How to verify
Run git branch -a. You should see * main and remotes/origin/main.
Step 3: Branch and Create New Commits
Before you ask an agent to write code, move off main. Creating a branch takes one second and gives you an isolated workspace.
Prompt for your agent
“Create and switch to a new branch called
feat/add-login-form. Then implement the login form, run the tests, and commit the changes with a clear message. Do not push yet.”
Commands underneath
git switch -c feat/add-login-form
# ... make edits or let the agent edit files ...
git status
git add .
git commit -m "feat: add login form validation"
Why stage (git add) before committing? Staging lets you choose which modified files belong in the snapshot. Running git status first prevents you from accidentally committing .env secrets or temporary logs.
How to verify
Run git log -n 2 --oneline. Your new commit should appear at the top, and git branch --show-current should print feat/add-login-form, not main.
Step 4: Open a Pull Request (and Why It Exists)
A Pull Request (PR) is a proposal on GitHub: “Here are the commits on my branch; please review them before merging into main.”
Why not push straight to main? Three reasons:
- Automated checks run first: GitHub Actions, build previews, and linters verify your code before it hits
main. - A diff you can actually read: You (or a teammate, or an AI reviewer) see every changed line side by side.
- Safe agent collaboration: Coding agents like Copilot, Claude, and Codex can open draft pull requests from VS Code or GitHub.com, and Agent Merge can even watch a PR and fix failing checks or merge conflicts in the cloud before merging.
Prompt for your agent
“Push
feat/add-login-formtooriginand open a pull request againstmainsummarizing what changed and how we tested it. Print the PR URL.”
Commands underneath
git push -u origin HEAD
gh pr create --base main --title "feat: add login form" --body "Adds login form and validation tests."
When do you merge to main?
Merge only when all three conditions hold:
| Check | Question to answer | How to verify |
|---|---|---|
| Build & CI pass | Are all required checks green? | gh pr checks (or PR page on GitHub) |
| Diff is reviewed | Did you (or a reviewer) read the changed files? | gh pr diff or GitHub Files changed tab |
| Branch is up to date | Are there merge conflicts with main? | GitHub merge box says No conflicts |
Once ready, merge and delete the remote feature branch:
gh pr merge --squash --delete-branch
If you are automating a workflow with an agent, GitHub CLI v2.105.0+ also introduced gh event listen (in preview with Event Streams) so scripts can wait for a review or check suite instead of polling in a loop (GitHub Changelog, 2026-11-18).
Step 5: Sync main Locally and Start Over
This is the step most beginners skip—and the reason their next branch ends up full of conflicts. Merging a PR updates main on GitHub, not on your laptop.
flowchart TD
S["Start: PR merged on GitHub"] --> Q{"Need to clean up\na local mess instead?"}
Q -- "No: normal post-merge cycle" --> M["git switch main\ngit pull origin main\ngit branch -d feat/old"]
M --> N["git switch -c feat/next-task"]
Q -- "Yes: edited main by mistake" --> R1["git switch -c feat/rescue\n(moves uncommitted work off main)"]
Q -- "Yes: want to discard local edits" --> R2["git restore .\n(erases unsaved file edits)"]
Q -- "Yes: undo last local commit" --> R3["git reset --soft HEAD~1\n(keeps files, undoes commit)"]
A. Normal “start over” after a PR merges
Prompt for your agent
“The PR is merged. Switch back to
main, pull the latest changes fromorigin, delete the localfeat/add-login-formbranch, and confirm our working tree is clean.”
Commands underneath
git switch main
git pull origin main
git branch -d feat/add-login-form
Now your local main matches GitHub, and you are ready to run git switch -c feat/next-task for the next feature.
B. Emergency “start over” when things go wrong locally
If an agent broke half your app or you made changes in the wrong place, pick the recovery move that matches your situation:
| Situation | Prompt for your agent | Command underneath |
|---|---|---|
| Discard all uncommitted edits | ”Discard all uncommitted changes in the working tree so we match the last commit.” | git restore . |
Accidentally worked on main | ”Move my current uncommitted changes onto a new branch called feat/rescue without losing them.” | git switch -c feat/rescue |
| Undo the last local commit (keep code) | “Undo the last commit but keep all the file changes staged so we can fix the message or split it.” | git reset --soft HEAD~1 |
| Throw away a broken local branch | ”Switch to main and force-delete the local branch feat/broken-experiment.” | git switch main && git branch -D feat/broken-experiment |
Rule of thumb: Never run
git reset --hardorgit push --forceunless you are 100% sure you want to permanently erase commits. When in doubt, ask your agent to inspectgit statusandgit log -n 5 --onelinefirst.
Working as a Team (and Inviting Reviewers to a Private Repo)
When a second person—a teammate, a mentor, or another engineer—joins your project, the five-step loop stays the same. Each person creates their own branch, opens a PR, and reviews the other person’s diff before merging into main.
If your repository is private, strangers cannot see it or open your PR links until you invite them as collaborators.
1. Invite a collaborator to a private repository
Prompt for your agent
“Add
@teammateas a collaborator on my private repo<owner>/<repo>so they can review pull requests, and check if the invitation is pending.”
Via GitHub UI or CLI
In the GitHub web UI, open your repository and go to Settings → Collaborators → Add people, type their GitHub username or email, and send the invite (Inviting collaborators to a personal repository). They must accept the email invitation before they can view or clone the repo.
Or invite them from the terminal with gh:
gh api -X PUT repos/<owner>/<repo>/collaborators/<username>
gh api repos/<owner>/<repo>/invitations
Important caveat for personal accounts: On a private repository owned by a personal GitHub account, collaborators automatically receive write access (there is no read-only collaborator role). If you need read-only reviewers or fine-grained permissions, move the repository into a GitHub Organization (Permission levels for a personal account repository).
2. Request a review and iterate on feedback
Once your teammate accepts the invite, assign them to your PR instead of pasting links in chat and hoping they notice:
Prompt for your agent
“Request a review from
@teammateon my current pull request. When they leave comments, summarize what they asked for, fix the code on this same branch, and push a new commit.”
Commands underneath
gh pr edit --add-reviewer <username>
# ... make the requested changes on the same branch ...
git add .
git commit -m "fix: address review feedback on validation"
git push
Every new commit you push to that branch updates the open PR automatically—you do not open a second PR for review fixes. Once your teammate approves and checks are green, merge the PR and sync your local main as usual.
Common Mistakes
- Prompting a new feature on
main: Always start a session with “Checkgit branch --show-current; if we are onmain, create a new branch first.” - Forgetting
git pullafter merging a PR: Your localmainfalls behind GitHub, so your next branch starts from stale code. - Letting an agent commit
.envor secrets: Ask the agent to verify.gitignorebefore runninggit add .. - Running parallel agents in the same folder: Two agents editing the same working directory overwrite each other. Use separate Git worktrees (supported natively in GitHub Desktop 3.6 and VS Code) when running concurrent tasks.
Sources
- Quickstart for repositories — GitHub Docs
- About Git — GitHub Docs
- Merging a pull request — GitHub Docs
- Inviting collaborators to a personal repository — GitHub Docs
- Permission levels for a personal account repository — GitHub Docs
- GitHub Desktop 3.6: Worktrees and deeper Copilot integration — GitHub Changelog (2026-06-26)
- GitHub Copilot in Visual Studio Code, July 2026 releases — GitHub Changelog (2026-07-30)
- Agent Merge watches and fixes pull requests in the cloud — GitHub Changelog (2026-10-28)
- Wait for pull request events with GitHub CLI — GitHub Changelog (2026-11-18)