Skip to main content

Introduction to Git

A hands-on Git tutorial in 10 parts: commits, branches, merge conflicts, remotes, pull requests, rebase, bisect, signing and Git 2.56, with real output.

Git records every change to a project, lets several people work on it at once, and merges their work back together. It is the version control system behind almost every software project, and behind most infrastructure code, documentation and configuration too. You can learn a dozen commands in an afternoon; using Git well, without fear of losing work, takes a clear picture of what those commands do.

This guide gives you that picture with one project from start to finish. In Part 2 you create status-page, a small status page for a team's services. Over the next parts it gets branches, merges and conflicts, a remote server and a second developer called Sam, pull requests and a release branch, a bug found with git bisect, a leaked token removed from the history, signed commits, hooks and a submodule. Every command was run for real, and the output you see is what Git printed.

What You'll Learn

  1. Git Fundamentals: snapshots, the working tree, staging area and repository, installing Git 2.56, first-time setup, and what changes in Git 3.0.
  2. Creating Repositories: git init, the first commit, what is inside .git, .gitignore, and git clone with --branch, --depth and --filter.
  3. Tracking Changes: git status, git diff, git add -p, commits, git log, renames, undoing mistakes with restore and --amend, and tags.
  4. Branching: what a branch is on disk, git switch, uncommitted changes and git stash, detached HEAD, and cleaning up branches.
  5. Merging and Rebasing: fast-forward and merge commits, resolving a conflict step by step, zdiff3 markers, git rebase, squash merges, and merge versus rebase.
  6. Remote Repositories: push, fetch and pull, rejected pushes, tracking branches, safe force pushes, pruning, SSH keys and HTTPS logins, forks and push protection.
  7. Collaboration Workflows: the pull request flow with fixups and git range-diff, merge methods, stacked branches with --update-refs, backports, and choosing a workflow.
  8. Git History: searching the log, git blame, git bisect run, revert, reset and the reflog, interactive rebase, git history, and removing a secret with git filter-repo.
  9. Best Practices: commit messages and Conventional Commits, .gitattributes, keeping secrets out, hooks, SSH commit signing, settings and aliases, maintenance.
  10. Advanced Techniques: Git objects, SHA-256 and reftable, worktrees, partial clones and sparse checkout, rerere, submodules and subtrees, archives, bundles and scripting.

Git Commands at a Glance

You want to Command Part
Start a repository, or copy one git init, git clone <url> 2
See what changed git status, git diff, git diff --staged 3
Save a change git add -p, git commit 3
Undo local changes git restore, git restore --staged, git commit --amend 3
Work on something separately git switch -c <branch>, git stash 4
Combine branches git merge, git rebase 5
Share work git push -u, git fetch, git pull --rebase 6
Force-push without losing others' work git push --force-with-lease --force-if-includes 6
Clean up a branch before review git commit --fixup, git rebase -i --autosquash 7, 8
Find who changed a line, or which commit broke something git blame, git bisect run 8
Undo a pushed commit git revert 8
Get back something you lost git reflog 8
Prove that commits are yours gpg.format ssh, commit.gpgSign true 9
Clone a huge repository git clone --filter=blob:none --sparse 10

Which Versions This Guide Uses

The guide was updated on 24 October 2026 for Git 2.56.0, the current release (28 September 2026). The terminal output comes from real runs of Git 2.56.0 built from the release tarball, on Linux, as a user called dev with the name "Dev" and the email dev@example.com, on a machine called devbox. Commit dates were fixed to 12 to 22 October 2026, so that the hashes in one part match the hashes in the next. A bare repository in ~/server plays the hosting service, and a second clone plays Sam.

  • Where Git opens an editor or asks a question (git add -p, git rebase -i, ssh-keygen), the answers were typed by a script, and the text tells you what to enter. The output is what Git printed.
  • Not run: commands that need a hosting service or another operating system: gh, ssh -T git@github.com, GitHub settings, the Windows and macOS install commands, Git LFS, and git maintenance start (which needs a system scheduler). They are shown as code without output, or labelled as an example.

If you learned Git from an older tutorial, these are the changes worth knowing:

Old habit Now Why
git checkout -b <branch> git switch -c <branch> One command per job; stable since Git 2.51
git checkout -- <file>, git reset HEAD <file> git restore <file>, git restore --staged <file> Clear names for "discard" and "unstage"
A master branch in new repositories git config --global init.defaultBranch main Matches GitHub and GitLab; the default in Git 3.0
git push --force git push --force-with-lease --force-if-includes Does not delete commits you have not seen
git pull without a strategy pull.rebase true (or pull.ff only) Git refuses to guess when branches diverged
git filter-branch, BFG git filter-repo Faster, safer, recommended by the Git project
GPG keys for signing SSH keys with gpg.format ssh You already have an SSH key
git config --list, git config <key> <value> git config list, git config set <key> <value> Subcommands since Git 2.46; the old forms still work
Hooks only in .git/hooks hook.<name>.command in the config, or core.hooksPath Several hooks per event, defined in config (Git 2.54+)
-s recursive The default ort strategy recursive is only an alias for ort since Git 2.50

Prerequisites

  • A computer with Linux, macOS or Windows (Git Bash or WSL on Windows).
  • Basic terminal use: cd, ls, running a command. The Linux command line part of the Linux guide covers it, and the Bash guide goes further.
  • No account on GitHub or GitLab is needed until Part 6, and even there everything runs locally first.

Practice Hands-On

Follow the parts in order and type the commands: you get the same project and, apart from hashes and dates, the same output. To see commits, branches and merges as a graph without installing anything, try the Git Concepts simulator, and practice conflicts in the Git merge conflict simulator.

Keep Learning

Start with Part 1: Git Fundamentals, or jump to merge conflicts or undoing commits if you need that answer now.

Last updated:

Tags

GitVersion ControlGitHubBranchingCollaborationDevOps

Found an issue?