Guide / Tutorial

Learn Git Before Your AI Breaks Your Code: A Practical Guide

AI agents can write thousands of lines in an afternoon. This guide walks through the Git and GitHub workflow that lets you save, inspect, undo and safely review everything they change.

Claude CodeThis guide is about Claude Code.
Learn Git Before Your AI Breaks Your Code: A Practical Guide

A structured, illustrated guide to the Git and GitHub commands you actually need when AI agents write most of your code: save points, undo, branches, remotes, issues and pull requests.

Reading map

  • Why Git now — AI writes code faster than anyone can read it; Git is how you go back.
  • Save points — configure Git once, initialize a repository, stage and commit.
  • See and undo — diff, staged diff, HEAD, restore, checkout an old commit and return.
  • Branches — the “sacred timeline” and safe alternate timelines for experiments and agents.
  • GitHub — remotes, private repositories, protecting secrets with .gitignore, push, clone and pull.
  • Agents and pull requests — issues as a to-do list, agents working on branches, review, merge, conflicts and revert.

Why Git Matters More in the AI-Coding Era

In the original lesson, NetworkChuck opens with a real pull request from an app he is building: an AI produced a change touching 43 files and roughly 4,000 lines of code. Nobody reads that line by line. Sooner or later an AI-written change breaks something, and the only question that matters is whether you can get back to a version that worked.

An AI-written pull request: 43 files changed, about 4,000 lines
An AI-written pull request: 43 files changed, about 4,000 lines • 00:01:49

Git answers that question. The simplest way to think about it is the save-game feature in a video game: before a dangerous section you save, and if things go wrong you load the save. Every commit is a save point for your project.

Git as a save-game system for code
Git as a save-game system for code

Git is not GitHub

The two names are often used as if they were one thing. They are not:

Git GitHub
What it is A free version-control tool that runs on your computer A website that stores Git repositories online
Made by Linus Torvalds, the creator of Linux A company (one of several hosts)
Works offline Yes, everything in this guide’s first three parts No, it is the remote copy
Alternatives — GitLab, Bitbucket, self-hosted Gitea
Git and GitHub are different things
Git and GitHub are different things

Part I — Save Points: Your First Repository

1. Check that Git is installed

Run git --version in a terminal. On most Macs and Linux machines Git is already installed; on Windows it can be installed with WinGet. If you see a version number, you are ready.

Checking the installed Git version
Checking the installed Git version • 00:03:13

2. Tell Git who you are (once per computer)

Every commit is signed with a name and an email, and new repositories should start on a branch called main:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

These three settings are global, so you only set them once per computer.

The three global settings, as typed in the lesson
The three global settings, as typed in the lesson • 00:04:21

3. Turn a folder into a repository

Create a project folder, add a couple of files, and run git init. It looks as if nothing happened, but a hidden .git folder now exists. That folder holds the entire history of the project; deleting it deletes the history.

mkdir git-gud && cd git-gud
git init
ls -la        # the hidden .git folder appears
git init creates the hidden .git folder
git init creates the hidden .git folder • 00:04:46

4. Read the situation with git status

git status is the command you will type most often. In a fresh repository it reports the current branch (main), that there are no commits yet, and that your new files are untracked: Git can see them but is not recording their history.

git status: untracked files on a new repository
git status: untracked files on a new repository • 00:05:13

5. Stage changes with git add

git add index.html stages one file; git add . stages everything in the folder. Staging is a waiting area between your working files and the official history. The lesson’s analogy works well: you have written the letter and put it in an envelope with a stamp, but it is not in the mailbox yet.

The staging area, as a letter in an envelope
The staging area, as a letter in an envelope

6. Commit: put the letter in the mailbox

git commit -m "my first commit" records the staged changes as a save point with a short message. git log shows the history in detail (author, date, message, hash), and git log --oneline gives a compact list that is easier to scan.

git log shows the commit hash, author, date and message
git log shows the commit hash, author, date and message • 00:06:43

Commit small and often. Each commit should be a state you would be happy to return to.

Part II — See and Undo

7. git diff shows exactly what changed

After editing files, git diff lists every change: removed lines in red, added lines in green, down to a single deleted comma. This is the fastest way to see what an AI agent actually did to your files.

git diff marks removed and added lines
git diff marks removed and added lines • 00:07:42

8. Staged changes need –staged

Once you run git add, the changes disappear from plain git diff because they have moved to the staging area. Use git diff --staged to review them before committing. Then commit them as the next save point.

git add .
git diff            # shows nothing now
git diff --staged   # shows what will be committed
git commit -m "first change"

9. HEAD is the “you are here” marker

In git log --oneline you will see HEAD -> main next to the latest commit. HEAD is like the “you are here” arrow on a shopping-mall map: it marks the save point you are currently on.

HEAD marks the commit you are on
HEAD marks the commit you are on

10. Undo a broken file with git restore

If you break a tracked file, git restore <file> replaces it with the version from the last commit, instantly. This only affects changes you have not committed yet.

git restore brings back the last saved version
git restore brings back the last saved version • 00:09:16

11. Travel back to an old commit and return

Copy the hash of an older commit from git log --oneline and run git checkout <hash>. Your files now look exactly as they did at that save point, and the newer commits are hidden from view. Git calls this a “detached HEAD”: fine for looking around, not for new work.

To return to the present, run git switch main. Nothing was lost; every commit still lives in the .git folder.

Checking out an old commit
Checking out an old commit • 00:10:16
Situation Command Effect
A file is broken, not committed yet git restore file Back to the last committed version
You want to look at an old version git checkout <hash> Files match that commit (detached HEAD)
Back to the latest work git switch main Return to the tip of main

Part III — Branches: A Safe Place to Break Things

12. main is the sacred timeline

The lesson calls main the sacred timeline: the version you trust. A branch is an alternate timeline that starts from the same history, where you can experiment without touching main.

main as the trusted timeline, a branch as the safe alternative
main as the trusted timeline, a branch as the safe alternative

13. Create a branch and move onto it

git branch zombie-attack     # create the branch
git switch zombie-attack     # move onto it
git branch                   # the * shows where you are

The files and history are the same at first. Commits you make now move only the branch forward.

Creating and switching to a branch
Creating and switching to a branch • 00:12:31

14. Two timelines side by side

Commit a change on zombie-attack, then git switch main: the change is not there. Switch back and it reappears. git log --oneline shows the branch one commit ahead of main.

The branch moves ahead while main stays unchanged
The branch moves ahead while main stays unchanged

15. Merge when you like the result

When the experiment is good, switch to main and merge it:

git switch main
git merge zombie-attack
git branch -d zombie-attack   # the branch has done its job

When main has not changed in the meantime, Git simply moves main forward to the branch’s latest commit (a “fast-forward”).

A fast-forward merge onto the sacred timeline
A fast-forward merge onto the sacred timeline • 00:14:16

The rule for AI agents: let them work on a branch, never directly on main.

Part IV — GitHub: Back Up, Share and Work Together

16. Why a remote

Everything so far lives in one folder on one computer. A remote copy lets you back up the project, share it, and work with other people or agents on other machines. GitHub is the best known host; GitLab and Bitbucket work the same way.

17. GitHub CLI: log in and create a private repository

gh --version
gh auth login
gh repo create git-gud --private

gh auth login walks through a browser login once. gh repo create --private makes an empty repository that only you can see.

Logging in with the GitHub CLI
Logging in with the GitHub CLI • 00:16:36

18. Protect secrets with .gitignore before the first push

Projects often keep passwords and API keys in a .env file. Anything pushed to GitHub may be seen by others and stays in history, so list such files in .gitignore first. Git will then never upload them.

nano .gitignore
.env
Protecting the .env file with .gitignore
Protecting the .env file with .gitignore

19. Connect, push, clone and pull

git remote add origin https://github.com/you/git-gud.git
git push -u origin main     # first push; -u remembers the target
git push                    # every later push
git clone https://github.com/you/git-gud.git   # another computer
git pull                    # bring down changes made elsewhere

After the first push, refresh the repository page: every commit is there, and the .env file is not.

Pushing to GitHub for the first time
Pushing to GitHub for the first time • 00:18:59
The repository on GitHub, without the .env file
The repository on GitHub, without the .env file • 00:19:18

Part V — Agents and Pull Requests

20. Issues are GitHub’s to-do list

An issue is a task written down in the repository: a title and a body that says what to do. They can be created from the terminal:

gh issue create --title "Add a line to the story" --body "Add one scary line"
gh issue list
Creating an issue from the terminal
Creating an issue from the terminal • 00:23:09

21. Let an agent work the issue on a branch

In the lesson, a coding agent is started in a second copy of the repository and told: work issue number one, in a branch. It uses the same tools covered above (Git and the GitHub CLI), commits on its own branch and pushes that branch to GitHub.

22. Fetch the agent’s branch without merging anything

git fetch
git branch -a                  # lists remote branches too
git switch issue-1-branch      # look around safely

git fetch downloads the new branch but does not change your main. Switching to it lets you inspect what the agent did.

git fetch shows the agent's new branch
git fetch shows the agent’s new branch • 00:26:13

23. Pull requests: “please merge my branch”

A pull request is a GitHub feature, not a Git one: a request to merge one branch into another, with a page for review. The agent can open it with gh pr create, and if it references the issue, merging the pull request closes the issue automatically.

gh pr create
gh pr view 3
gh pr merge 4 --merge
git pull

Review first, merge on GitHub or with gh pr merge, then git pull so your local copy has the result, and delete the finished branch.

A pull request that closes its issue
A pull request that closes its issue • 00:28:26

24. Conflicts: both sides changed the same lines

If you change main while the agent works on its branch, the two versions can collide. GitHub shows both changes side by side; you keep yours, keep theirs, or keep both, then commit the merge.

Resolving a conflict in GitHub's editor
Resolving a conflict in GitHub’s editor • 00:33:38

25. Undo a merged pull request with revert

If a merged change turns out to be bad, open the closed pull request and click Revert. GitHub creates a new pull request that undoes the change, which you merge like any other.

Reverting a merged pull request
Reverting a merged pull request • 00:34:52

Final Perspective — Three Habits That Matter

Habit Why it matters with AI agents
Commit small and often Every commit is a known-good state you can return to
Agents work on branches Main stays trustworthy while experiments run
Read every pull request before merging You stay the reviewer, not a rubber stamp

With these habits, nothing an AI writes can sink your project: you can always see the change, refuse it, or undo it.

The three habits
The three habits

Command Quick Reference

Goal Command
Set identity (once) git config --global user.name/user.email
Start a repository git init
See what is going on git status, git log --oneline
Stage and save git add ., git commit -m "message"
See changes git diff, git diff --staged
Undo an uncommitted file git restore file
Look at an old version git checkout <hash>, back with git switch main
Branches git branch name, git switch name, git merge name, git branch -d name
Remote gh repo create --private, git remote add origin URL, git push -u origin main
Other computers git clone URL, git pull, git fetch
Issues and PRs gh issue create, gh pr create, gh pr merge --merge

Further Reading and Source Notes

  • Source video: “you need to learn Git RIGHT NOW!!” by NetworkChuck
  • Video summary by AIBotRadar: “Learn Git in 10 Minutes (Before Your AI Breaks Your Code)”.
  • Screenshots are frames from the original lesson, shown for commentary and education with credit to NetworkChuck. Diagrams are original.
  • The original video was sponsored by CodeRabbit; the sponsor segment is not covered here.

Educational summary and commentary. Commands are shown as typed in the original lesson; check them against your own setup before using them on real projects.

Claude Code GUIDES

More Claude Code guides

Continue with practical guides about Claude Code.

View all AI Coding guides