Git and Version Control by kirupa (https://www.kirupa.com/me/index.htm) | filed under Build Essentials (https://www.kirupa.com/build/index.htm) TL;DR: Saving a labeled snapshot of your whole project every time it works, with a note, an author, and a link to the snapshot before it, lets you see exactly what changed and get back to any working version. That's version control, and Git is the tool most teams use for it. - Each snapshot is called a commit, and its ID is computed from its contents. A diff shows exactly which lines changed between two commits. - A branch is a parallel timeline for trying something risky. A pull request lets people and automated checks look at a branch before it merges into main. - A revert is a new commit that undoes an earlier one and keeps history intact. Rewriting history that others already have causes trouble. - Agents change a lot of files quickly, so give each task its own branch, keep commits small, and read the diff instead of the chat summary. It's the Saturday before Crumb & Co.'s holiday pie pre-orders close, and Wren, our bakery's web developer, asks an AI coding agent to tidy up the menu page. Two minutes later, the agent says it's done, and the menu looks great. An hour later, Rosa calls from the shop. Nobody has been able to pay for a pie since lunch. Wren has no idea which of the agent's changes broke checkout, and there's no copy of this morning's site to go back to. We are going to replay that afternoon with a setup that turns it into a five-minute fix, and we'll see why it matters even more when agents write our code. Onwards! A Folder Full of Finals OK, that isn't entirely true, because Wren does have a few copies of our menu page lying around: [Image: A gag. Wren's crumb-site folder holds four copies of the menu page as big file icons: menu.html, menu_final.html, menu_final_v2.html, and menu_final_v2_REALLY_final.html. A handwritten note points at the last one: Which one is live??] (https://www.kirupa.com/build/images/git_and_version_control_finals_folder.png) Saving copies by hand works for one file on a slow day. These copies only cover our menu page, though, and today's problem is in checkout. None of them say what changed or why, and they couldn't have kept up anyway, because our agent edited six files in about two minutes. What we really need is a way to save our whole project, with a note about what changed, every time our site is in a good state. Snapshots of the Whole Project Imagine taking a snapshot of every file in our project whenever everything works. Along with the files, our snapshot records a short note about what changed, who changed it, when, and which snapshot came right before it. Here is one of these snapshots for our bakery's site: [Image: One snapshot of the bakery's website, drawn like an instant photo. The photo holds every file in the crumb-site folder: checkout.html, index.html, menu.html, src/checkout.js, utils/prices.js, and 13 more files. Beside it are the snapshot's details: ID a41d7b9, note Add pie pre-orders, author Wren, when Nov 18, 4:12pm, and before this c71e5f0.] (https://www.kirupa.com/build/images/git_and_version_control_snapshot.png) This snapshot holds all of our site's files exactly as they were on November 18 at 4:12pm, when Wren finished the pie pre-order page. Its last line points back to the snapshot before it, which points back to the one before that, and so on: [Image: Four snapshots in a row, oldest on the left: 3f9c2e1, Start the bakery site, Oct 6; 8b04d6a, Add the menu page, Oct 13; c71e5f0, Add checkout, Oct 27; and a41d7b9, Add pie pre-orders, Nov 18. An arrow runs from each snapshot back to the one before it.] (https://www.kirupa.com/build/images/git_and_version_control_history.png) Following those arrows gives us the whole story of our site, newest to oldest. Each snapshot is called a commit, the chain of them is our history, and all of it lives in a hidden folder inside our project called a repository (aka repo). Saving labeled snapshots like this, so we can see and return to any earlier version, is called version control. The version control tool most of us use is Git, which Linus Torvalds started in April 2005 (https://git-scm.com/book/en/v2/Getting-Started-A-Short-History-of-Git) for the Linux kernel. Git's very first commit (https://github.com/git/git/commit/e83c5163316f89bfbde7d9ab23ca2e25604af290) has a note that sets the tone: Initial revision of "git", the information manager from hell. Note: Where Do Commit IDs Come From? Git runs everything in a commit, including our files, our note, and the previous commit's ID, through a hash function that turns it into a short fingerprint. That fingerprint is the commit's ID, so changing one character changes the ID. Git uses SHA-1 by default and also supports the stronger SHA-256 (https://git-scm.com/docs/hash-function-transition). Seeing Exactly What Changed Let's replay Saturday, and this time our agent's changes get saved as one more commit, e5b8c32. Wren also keeps a copy of our repository on GitHub, and our site updates whenever new commits land there. Git will compare any two commits and show us every line that's different. That comparison is called a diff, and here is the diff for our agent's tidy-up: [Image: The diff between a41d7b9 and e5b8c32, the agent's tidy-up. Six files changed: menu.html +18 −24, src/menu.js +6 −5, src/stock.js +1 −1, styles/menu.css +14 −27, styles/shared.css +2 −1, and utils/prices.js +1 −1, which is highlighted. Below, the change in utils/prices.js: inside function getPrice(item), the red line return item.priceCents; became the green line return formatDollars(item.priceCents);. A handwritten note points at it: Checkout uses this!] (https://www.kirupa.com/build/images/git_and_version_control_diff.png) (https://www.kirupa.com/build/images/git_and_version_control_diff.png) Five of the six files are about our menu page. The sixth, utils/prices.js, holds shared helpers that our checkout uses too. Our agent changed getPrice to return text like $24.00, which reads nicely on the menu, but checkout multiplies that price by the number of pies, and text times a number gives us NaN (Not a Number) instead of a total. So...nobody can pay. Undoing a Bad Commit Now that we know which commit broke checkout, we'll draw each commit as a dot on a timeline, and here is where our history stands: [Image: A timeline of commits, oldest on the left: c71e5f0 Add checkout, a41d7b9 Add pie pre-orders, and e5b8c32 Tidy up the menu page, drawn in red. A handwritten note points at it: Checkout broke here!] (https://www.kirupa.com/build/images/git_and_version_control_bad_commit.png) The safest way to undo e5b8c32 is to add one more commit that does the exact opposite. Every line our agent added will get removed, and every line it removed will come back: [Image: The same timeline with one more commit at the end: 7d2a9f4, Revert "Tidy up the menu page", drawn in yellow. e5b8c32 is still in the history with its note, Checkout broke here! A second note points at 7d2a9f4: Checkout works again!] (https://www.kirupa.com/build/images/git_and_version_control_revert.png) This new commit is called a revert (here are the git revert docs (https://git-scm.com/docs/git-revert)). Our code is back to how it was on November 18, and since the tidy-up is still in our history, Wren will fix getPrice on Monday and keep the nicer layout. Git can also rewind our history so e5b8c32 disappears entirely (that's git reset), which is fine for commits that only live on our laptop. The trouble is that Wren already copied e5b8c32 up to GitHub, which is called pushing, so rewinding now leaves us with two different histories: [Image: Two timelines. On Wren's laptop, after git reset: c71e5f0 and a41d7b9. On GitHub: c71e5f0, a41d7b9, and e5b8c32. A handwritten note points at GitHub's e5b8c32: Still on GitHub!] (https://www.kirupa.com/build/images/git_and_version_control_rewrite.png) To make GitHub match, Wren would have to overwrite it with git push --force, and anyone who already pulled e5b8c32 could push it right back without meaning to. Our rule is pretty simple. We revert anything that's been pushed, and we only rewrite history that nobody else has seen. Parallel Timelines Even better would be keeping our agent's experiments off the working site until someone has looked at them, and we are going to try that when Crumb & Co.'s owner asks for a gluten-free section on the menu. If you've seen Back to the Future Part II, you may remember Doc Brown's chalkboard, where old Biff's sports almanac skews the timeline into an alternate 1985. My redraw of it looks as follows: [Image: A gag: a redraw of the chalkboard from Back to the Future Part II. A chalk timeline runs from PAST through 1955 and 1985 to FUTURE. At 1955, a second line skews up into a tangent that ends at 1985A.] (https://www.kirupa.com/build/images/git_and_version_control_chalkboard.png) Git lets us do this on purpose (minus the evil Biff). Our main timeline keeps the version of the site that works, and we start a second timeline from its latest commit: [Image: The main timeline ends at 7d2a9f4, Revert "Tidy up the menu page", with faded dots for the older commits before it. Two tags point at 7d2a9f4: main, and a new one called gluten-free-menu.] (https://www.kirupa.com/build/images/git_and_version_control_branch_start.png) Each timeline is called a branch, and a branch is just a name that points at the newest commit on its timeline. Ours are called main (the default name GitHub uses) and gluten-free-menu. Here is what our two branches will look like once our agent commits on its branch and Wren fixes a typo on main: [Image: Two parallel timelines. The main timeline goes from 7d2a9f4 to 9a1f3c8, Fix hours typo, and the main tag points at 9a1f3c8. Below it, the gluten-free-menu timeline splits off from 7d2a9f4 and has two commits: b3c9e07, Add gluten-free section, and f18a6d2, Label gluten-free pies. The gluten-free-menu tag points at f18a6d2.] (https://www.kirupa.com/build/images/git_and_version_control_branch_diverge.png) Our live site is built from main, so customers won't see the gluten-free work until we bring it over. If our agent's first try is a mess, we can delete the branch, and main will never know. Asking Before We Merge Before our agent's branch joins main, we want someone (or something) to look at it. On GitHub, that happens on a page that proposes merging our branch into main: [Image: A pull request page titled Add a gluten-free menu, number 12, marked Open. Wren wants to merge 3 commits into main from gluten-free-menu. Files changed: 3 files, +38 −4. In the conversation, the owner says: Looks delicious! Can the gluten-free pies get a leaf icon? Wren's agent replies: Done in 4e7b1a0. Checks: all checks passed, tests 24 of 24. At the bottom is a green Merge pull request button, and a handwritten note points at it: Look before it lands!] (https://www.kirupa.com/build/images/git_and_version_control_pull_request.png) (https://www.kirupa.com/build/images/git_and_version_control_pull_request.png) That page is called a pull request, and GitHub's docs (https://docs.github.com/en/pull-requests/reference/pull-requests) describe it as a proposal to merge code changes, with the diff, the conversation, and the automated checks in one place. (GitLab (https://docs.gitlab.com/user/project/merge_requests/) calls it a merge request.) In ours, all 24 tests passed, and when the owner asked for a leaf icon, our agent added a commit to the branch. Bringing Timelines Back Together When Wren clicks Merge, Git will join our two timelines with a new commit that has two parents, one from each branch: [Image: The same two timelines after the merge. The gluten-free-menu timeline now has a third commit, 4e7b1a0, Add leaf icons, and then it rejoins main at a new commit, 6c2d8e5, Merge pull request #12, which has two parents: 9a1f3c8 on main and 4e7b1a0 on gluten-free-menu. The main tag points at 6c2d8e5, and the gluten-free-menu tag is gone.] (https://www.kirupa.com/build/images/git_and_version_control_merge.png) This is called a merge, and GitHub will then offer to delete the gluten-free-menu name. Our merge went smoothly because the two branches changed different lines, but what if Wren had marked the pecan pie as sold out on main while our agent renamed that same pie on its branch? Git would stop and ask us which version to keep: [Image: A merge conflict. Line 14 of menu.html used to say Pecan pie. On main, it was changed to Pecan pie (sold out). On gluten-free-menu, it was changed to Gluten-free pecan pie. Both lead to a warning: Conflict in menu.html. Which line do we keep?] (https://www.kirupa.com/build/images/git_and_version_control_conflict.png) That's called a merge conflict. We want both changes here, so we would edit line 14 to say Gluten-free pecan pie (sold out) and commit it. Conflicts sound scary, but most of them are just Git refusing to guess. Is Git a Backup? With all of these snapshots around, it's tempting to treat Git as a backup, so let's look at what our repository actually holds: [Image: Two panels. Wren's laptop holds the crumb-site repository with its commits. An arrow labeled git push copies those commits to GitHub, which holds the same commits. Below, a band labeled Not in the repository holds the orders database, uploaded photos, and .env secrets, which are listed in .gitignore.] (https://www.kirupa.com/build/images/git_and_version_control_not_backup.png) Until Wren pushes, new commits exist only on that laptop, and one spilled latte takes them all with it. Even after pushing, Git only has the files we told it to track. Our orders database (https://www.kirupa.com/build/data_modeling.htm), the photos customers upload with cake orders, and the .env file with our payment keys live outside the repository and need backups of their own. Listing a file like .env in a file called .gitignore tells Git to never pick it up. The opposite mistake is worse, because if a secret gets committed even once, deleting the file later doesn't take it out of our history: [Image: Three commits, oldest on the left. 5b0e9d2, Add payment keys, holds checkout.js and .env, which has a key icon. 1c8f4a7, Delete .env, holds only checkout.js. 0d3e6b1, Add .gitignore, holds checkout.js and .gitignore. A handwritten note points at the .env file in the oldest commit: Key still in here!] (https://www.kirupa.com/build/images/git_and_version_control_secret.png) Anyone with a copy of our repository can open that older commit and read the key. GitHub's guide to removing sensitive data (https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository) says the first step is to revoke or rotate the secret, which means we cancel the old key and make a new one. Why Agents Make This Matter Even More An agent can change six files in the time it takes us to read one. That's wonderful when the agent is right. When it's wrong, version control is how we find out what happened. Here is the summary our agent gave Wren on Saturday, next to the diff of the very same commit: [Image: On the left, Wren's agent in a chat: Done! I tidied up the menu page: cleaner layout, consistent spacing, and fewer duplicate styles. On the right, the diff of the same commit, e5b8c32: six files changed, menu.html, src/menu.js, src/stock.js, styles/menu.css, styles/shared.css, and utils/prices.js, which is highlighted. A handwritten note points at utils/prices.js: Not in the summary!] (https://www.kirupa.com/build/images/git_and_version_control_summary_vs_diff.png) The summary describes what our agent meant to do. The diff shows every line it actually changed, and on Saturday, the summary never mentioned prices.js. This is why I read the diff before I merge anything an agent writes. These days, we follow a few habits with every agent task: - Each task gets its own branch, so nothing reaches main without a pull request. - Commits stay small and get clear notes, so one revert undoes one thing. - Someone reads the diff, and the automated checks run on every pull request. - Our Copilot CLI setup (https://www.kirupa.com/ai_fundamentals/github_copilot_cli.htm) blocks git push, so agents can't rewrite our shared history. Our agents don't have to take turns, either. A project folder shows one branch at a time, but the git worktree (https://git-scm.com/docs/git-worktree) command can give one repository several folders, each with its own branch. These folders are called worktrees, and git worktree add ../crumb-gf gluten-free-menu is all it takes to make one: [Image: One crumb-site repository at the top, connected to three folders below it. crumb-site/ has Wren on the main branch. crumb-gf/ has an agent on the gluten-free-menu branch. crumb-hours/ has another agent on the holiday-hours branch.] (https://www.kirupa.com/build/images/git_and_version_control_worktrees.png) Two agents can now work at the same time without touching each other's files. The catch is that every one of these branches will merge into main eventually, and more agents means more conflicts to sort out. I'd start with two. The Same Saturday in Commands What we just did was walk through all of this as pictures, so here is Wren's replayed Saturday as commands in a terminal: $ git log --oneline e5b8c32 (HEAD -> main, origin/main) Tidy up the menu page a41d7b9 Add pie pre-orders c71e5f0 Add checkout 8b04d6a Add the menu page 3f9c2e1 Start the bakery site $ git revert --no-edit e5b8c32 [main 7d2a9f4] Revert "Tidy up the menu page" 6 files changed, 59 insertions(+), 42 deletions(-) $ git push To github.com:crumb-co/crumb-site.git e5b8c32..7d2a9f4 main -> main $ git switch -c gluten-free-menu Switched to a new branch 'gluten-free-menu' $ git add . $ git commit -m "Add gluten-free section" [gluten-free-menu b3c9e07] Add gluten-free section 2 files changed, 24 insertions(+), 2 deletions(-) The git log --oneline command lists our commits newest first. HEAD -> main means we're on main, and origin/main marks the newest commit GitHub has. Next, git revert makes our undo commit with the agent's numbers flipped around, and git switch -c creates our new branch and moves us onto it. Try It This Week Let's break something on purpose, in a scratch repository or on a branch of your own project: 1. Make a commit, and then make a deliberately bad one, like deleting a line your page needs. 2. Run git log --oneline and find your bad commit's ID. 3. Run git revert , look at git log --oneline again, and run git show to see the revert's diff. 4. Open a pull request for something small, even if you're the only reviewer, and read the whole diff before you merge. Frequently Asked Questions What Is the Difference Between Git and GitHub? Git is the version control tool that saves commits in a repository on our own computer. GitHub is a website that hosts copies of Git repositories so people can share them, and it adds pull requests, reviews, and automated checks on top. GitLab and Bitbucket do similar jobs. Is Git a Backup? Not by itself. Commits that have not been pushed exist on only one computer, and Git never stores databases, uploaded files, or anything listed in .gitignore. Push often, and back up everything else separately. What Is the Difference Between Git Revert and Git Reset? The git revert command adds a new commit that undoes an earlier one, so history stays intact and the fix is safe to share. The git reset command moves a branch back to an earlier commit, which removes the later commits from that branch. Use reset only on commits nobody else has, and revert anything that has been pushed. How Do I Remove a Secret I Committed to Git? Revoke or rotate the secret first, because anyone with a copy of the repository can still find it in an older commit. Then list the file in .gitignore so it is not committed again. Scrubbing the secret out of history is possible with tools like git-filter-repo, but everyone who has a copy has to clean up too. Conclusion Ok! It is time to wrap things up. Wren's bad Saturday happened because there was no record of what changed and no way back. Commits and diffs give us that record, and reverts give us the way back without erasing anything. Branches and pull requests keep our experiments off main until someone has looked at them. Git does have one honest limit. It tells us exactly what changed, and it has no idea whether the change is any good. That part is still on us, our tests, and our reviews, especially when an agent can write a week's worth of commits before lunch. As for Wren, the tidy-up finally shipped on Monday, with a getPrice that returns numbers again and a commit note that actually mentions prices.js. Before you go, here are a few things you can do: 1. Test your knowledge by taking the quiz below. 2. Check out some related content: - Getting Started with GitHub Copilot CLI (https://www.kirupa.com/ai_fundamentals/github_copilot_cli.htm). Set up a coding agent in your terminal and decide what it may do without asking. - Data Modeling and Databases (https://www.kirupa.com/build/data_modeling.htm). Shaping your app’s data, and changing it safely with migrations kept in version control. - Changelogs and Release Notes People Actually Read (https://www.kirupa.com/product_thinking/changelogs.htm). Turning what changed into notes people actually want to read. 3. Stuck on something, or curious about a detail we skipped? Ask on the forums (https://forum.kirupa.com/), and we’ll help you out. [Mastery Quiz] Test your knowledge! Take the Git and Version Control Quiz: https://www.kirupa.com/quizzes/git_and_version_control.htm Practice with 6 multiple-choice questions and an explanation after every answer. [/Mastery Quiz]