What Git is and how it works: commits save versions, reset jumps back, branches let you try ideas safely, merges join them, and GitHub shares your repo.
Git explained by cats. Two kitties write code. Kitty finishes feature, Kitty happy. Happy Kitty runs Git commit. Commit is a snapshot, a dot on the timeline. Second Kitty wants a new feature, creates a branch, a new path on the timeline.
Second Kitty, finish feature. Kitty use Git merge, path joined together. Want to share code? That's GitHub, a shared home for Git timelines. Push up, pull down, everyone stays in sync. Git is the tool. GitHub is the platform, not the same, but they work together.
Git records every version of your project, so you can go back to an earlier state when something breaks and share the work with other people. Most software teams use it, and it is one of the first tools a new developer has to learn.
The problem Git solves
Without version control, a project is one copy of each file. Change a function and the old version is gone. Break something and you have to remember what it looked like before. Teams end up emailing zip files around or keeping folders called final, final-v2 and final-really, and two people editing the same file overwrite each other's work.
Git keeps a history instead. Each time your work is in a good state, you save a version, and Git stores it alongside every version before it. That history records what changed, who changed it, when and why. It also lets you try an idea on its own line of work and throw it away if it fails, and lets several people change the same project while Git combines their work.
A folder that Git tracks is called a repository, or repo. You create one with git init, and Git keeps all of its history in a hidden .git folder inside it.
Commits: saving a version
A commit is one saved version of the project. It is a snapshot of every tracked file at that moment, plus a message describing the change, the author, the time and a link to the commit before it. Each commit gets an ID, a long string of letters and numbers worked out from its contents, which Git usually shows cut down to the first seven characters.
Saving a commit takes two steps. First you choose which changes go in with git add, which puts them in the staging area. Then git commit saves everything staged as a new version:
Git explained by cats. Two kitties write code. Kitty finishes feature, Kitty happy. Happy Kitty runs Git commit. Commit is a snapshot, a dot on the timeline. Second Kitty wants a new feature, creates a branch, a new path on the timeline.
Second Kitty, finish feature. Kitty use Git merge, path joined together. Want to share code? That's GitHub, a shared home for Git timelines. Push up, pull down, everyone stays in sync. Git is the tool. GitHub is the platform, not the same, but they work together.
The staging area exists so that one commit holds one logical change. If you fixed a typo and started a new feature in the same sitting, you can stage and commit the typo fix on its own, then commit the feature later.
Good commits are small, and their messages explain why the change was made. 'Fix crash when the basket is empty' tells a teammate far more in six months' time than 'update code'. git log lists the history, newest first:
Because every commit is still stored, you can return to one. git reset moves your current branch back to an earlier commit, so the commits after it are no longer part of that branch.
git reset --hard 9f8e7d6
The mode decides what happens to your files:
Mode
Branch moves back
Your changes
--soft
Yes
Kept and staged
--mixed (default)
Yes
Kept, not staged
--hard
Yes
Thrown away
--soft and --mixed help when you want to redo the last few commits differently: the work is still there, just uncommitted. --hard makes your files match the old commit exactly, and it also throws away any work you hadn't committed, so check git status before you use it.
Reset rewrites history. That is fine for commits that only exist on your machine. For commits you have already shared, use git revert instead: it adds a new commit that undoes an earlier one, so the history everyone else has stays intact.
Branches: separate lines of work
A branch is an independent line of commits. Your repository starts with one, usually called main. When you want to try something, you create a new branch and commit there, and main doesn't change until you decide it should.
A branch costs almost nothing: it is a named pointer to a commit, and each commit you make on it moves that pointer forward. That is why many teams create a branch for every feature or fix.
git switch changes which branch you are on, and Git updates the files in your folder to match. Older guides use git checkout -b for the same thing. To have two branches open at once in separate folders, see Git Worktrees.
Branches also keep people out of each other's way. Two developers on different features each have their own branch, so neither has to wait for the other, and half-finished code stays off the main line.
Merging branches together
When the work on a branch is ready, you merge it back into main:
git switch maingit merge twist-ending
Here is the history of a small project where a branch splits off main, both carry on, and the branch is merged back in:
Drawing the diagram…
Git merges in one of two ways. If main hasn't moved since the branch was made, Git moves main forward to the branch's last commit. This is a fast-forward. If both have new commits, as in the history above, Git creates a merge commit with two parents that brings both sets of changes together.
Most of the time Git combines the changes by itself. When both branches changed the same lines of the same file, it can't know which version you want, so it stops with a merge conflict. It marks the clashing lines in the file; you edit them into the version you want, then git add the file and git commit to finish the merge. Conflicts are a normal part of teamwork, and small, short-lived branches keep them rare. How a team organises its branches is a topic of its own, covered in Branching Strategies.
Git and GitHub are not the same thing
Git runs on your computer. Your repository and its full history live in that folder, and every command so far works without an internet connection.
GitHub is a website that hosts Git repositories. It gives a team one shared copy to sync with, and adds features Git doesn't have: pull requests for reviewing changes, issues for tracking work, and automation that runs on every push. GitLab and Bitbucket do the same job.
The shared copy is called a remote, usually named origin. Three commands move work between your machine and it:
git clone copies a remote repository to your machine, history and all.
git push sends your new commits up to the remote.
git pull fetches the commits other people pushed and merges them into your branch.
Here is a project shared between you and a teammate:
# You, once: link the repo and upload itgit remote add origin <repo-url>git push -u origin main# Your teammate: copy it, add a chaptergit clone <repo-url>cd cat-storygit add chapter-3.mdgit commit -m "Write chapter 3"git push# You: get your teammate's workgit pull
If someone else pushed first, Git refuses your push, because the remote has commits you don't have yet. Pull, sort out any conflicts, then push again.
Every clone holds the whole history, which is why Git is called a distributed version control system. If GitHub went down, each developer would still have a complete copy of the project.
Common mistakes
Committing everything at once: one giant commit called 'changes' is hard to review and impossible to undo in part. Commit each logical change on its own.
Running git reset --hard on shared commits: your teammates still have the commits you removed, and the next push or pull gets messy. Use git revert for anything already pushed.
Working directly on main: a branch per change keeps main working and makes each change easy to review.
Leaving branches open for weeks: the longer a branch lives, the further main moves on and the bigger the conflicts when you merge. Merge often.
Committing secrets: a password committed once stays in the history even after you delete the file. Keep secrets out of the repo, and list files such as .env in .gitignore.
Key takeaways
Git records your project's history as commits, each a snapshot with a message.
git reset moves a branch back to an earlier commit; git revert safely undoes a commit you have shared.
A branch is a cheap pointer to a line of commits, so experiments never touch main until you merge them.
Merging combines branches, and Git only asks for your help where both changed the same lines.
Git saves the versions; GitHub hosts a shared copy so a team can push, pull and work together.
What should we explain next?
Got a concept you want explained with cats? Tell us.