Tutorial

Git basics: save points for code

Git is save points for code. Every commit is a snapshot you can return to, which means you can experiment freely: nothing is ever truly lost.

What Git actually is

Git is a distributed version control system created by Linus Torvalds in April 2005 to manage Linux kernel development. "Distributed" is the key word: unlike older systems, every copy of a Git project contains the complete history. Your laptop holds the whole timeline, not just the latest files. That is why you can commit, branch, and browse history with no internet connection.

It is now the most widely used version control system among professional developers, by a large margin.

Git is not GitHub

This confuses every beginner exactly once. Git is the tool on your computer that tracks changes. GitHub (and GitLab, Bitbucket) is a website that hosts Git repositories so teams can share them. You can use Git for years without GitHub; you can't use GitHub without Git.

The core loop: three places

Your files live in three states, and Git moves them between these states:

  1. Working directory: your actual files, where you edit.
  2. Staging area: the loading dock. git add puts changed files here.
  3. Repository: permanent history. git commit seals the staged changes into a snapshot.

Think of it as: edit, gather what changed, take the snapshot.

Six commands that cover daily Git

CommandWhat it does
git initStart tracking a folder (once per project)
git add .Stage all changed files
git commit -m "message"Save the snapshot with a description of what changed
git statusShow what's changed and what's staged (run this constantly)
git logBrowse the history of snapshots
git pushSend your commits to the shared remote (e.g. GitHub)

Write commit messages for your future self: "fix login redirect on mobile" beats "fix stuff" every time.

Branches: parallel universes

A branch is a separate timeline. You create one to try something risky, commit freely there, and merge it back when it works, or delete it when it doesn't. The main timeline stays safe the whole time. This is how teams work on ten features at once without stepping on each other:

git branch new-feature      # create the timeline
git checkout new-feature    # step into it
# ... work, commit ...
git checkout main           # step back
git merge new-feature       # bring the work in

When you mess up

You will. Everyone does, including people with twenty years of experience. Two rules: first, don't panic-delete the folder; Git almost always has your work somewhere in its history. Second, ask before force-pushing to a shared branch; rewriting shared history is the one genuinely dangerous operation. For solo work, mistakes are cheap: that's the whole point of save points.

Worked example: your first afternoon on a real project

You join a small project and your lead asks you to fix the contact page heading. You start inside the project folder on your computer.

You run git init only if the folder has no Git history yet. On an existing project you would clone it instead, so check first with git status. When you run git init in a new folder, Git prints a line that says it initialized an empty repository and names the .git folder it created. That hidden folder holds the history.

git status
git add .
git commit -m "Add site files"

git status prints the branch name and lists files Git has never tracked. After git add, a second git status shows those files under changes to be committed. git commit prints a short summary with the branch name, a short hash, your message, and a count of files changed. The project now has its first save point.

Next you create a branch for the heading change so main stays untouched while you work.

git checkout -b fix-contact-heading
# edit the heading in your editor and save the file
git add .
git commit -m "Fix contact page heading"

git checkout with the b flag creates the branch and moves you onto it in one step. Git prints a line confirming the switch. You edit the file, stage it, and commit. The commit output looks like the first one, with a new hash and your new message.

When the heading looks right, you merge the branch back:

git checkout main
git merge fix-contact-heading
git branch -d fix-contact-heading

Switching to main prints the branch name. git merge prints an update line with the two hashes it connected and a short list of files changed. If main had no new commits while you worked, Git calls this a fast forward and simply moves main forward to your commit. Deleting the branch at the end prints a confirmation with the hash the branch pointed to. The work now lives on main.

Merge conflicts and how to resolve one

A conflict happens when two branches change the same lines in the same file. Git stops the merge and marks the file so you can decide. git status lists the file under unmerged paths, and git merge prints a line that says the merge failed and asks you to fix the conflicts.

Inside the file, the marked section looks like this:

<<<<<<< HEAD
<h1>Contact us</h1>
=======
<h1>Get in touch</h1>
>>>>>>> fix-contact-heading

The line with seven less than signs and HEAD marks the start of the version on your current branch. The line of equals signs separates the two versions. The line with seven greater than signs and the branch name marks the end of the incoming version. Everything between the first marker and the equals line is one version. Everything between the equals line and the last marker is the other version.

To resolve the conflict, edit the section into the final text you want. Keep one heading or write a new one that combines both edits. Delete all three marker lines and delete the version you are discarding. Read the file top to bottom to confirm no markers remain. A quick search for the marker characters in your editor catches leftovers.

Then you finish the merge:

git add contact.html
git commit -m "Merge fix-contact-heading and resolve heading conflict"

Staging the edited file tells Git the conflict is resolved. The commit seals the merge. Run git status once more. It should report a clean working tree, which means no conflict remains and no change is waiting.

Frequently asked questions

Do I need GitHub to learn Git?

No. Everything above works locally. Add GitHub later when you want backup, sharing, or a portfolio.

Should I commit broken code?

On a private branch, commit freely; that's what branches are for. Keep the shared main branch working.

What's the difference between Git and "save as v2-final-FINAL"?

Git records what changed and why, lets you compare any two moments, and merges parallel work. Folders full of renamed copies do none of that.

Last verified: October 22, 2026, against the official Git documentation. Git's core workflow is stable; the history above is settled fact.

Want professional workflows from day one?

We work in version control with staging environments on every project.

Get a quote →

← All tutorials