Chapter 6 · Lesson 1 of 10
Git for deployers

What is Git and why servers use it for deploys

Git is a version control system that records every change to your files as a commit, so a server can fetch an exact version of your code and you can return to any earlier one.

By the end you will

Explain what a commit is, how Git history works, and why deploys pull from Git instead of copying files by hand.

Time
10 min
Level
Beginner
Check
4 questions

Git is a version control system. It stores your project as a series of snapshots called commits, each with a message and a link to the commit before it. Servers and deploy tools use Git because a commit names one exact version of your code. If you deploy commit a1b2c3d today and the same commit next week, you get identical files, and if a release breaks something you deploy the previous commit again.

How it works#

A Git repository is a folder with your project files plus a hidden .git directory. The .git directory holds the whole history. Nothing is stored on a central server unless you choose to add one, so every copy of a repository is a full copy.

Each commit contains:

  • A snapshot of every tracked file at that moment.
  • The author, the date and a short message you wrote.
  • A pointer to the parent commit (the one before it).
  • An ID, called a hash, such as a1b2c3d4e5f6.... The hash is computed from the content, so changing anything about a commit changes its hash.

Because each commit points to its parent, commits form a chain. That chain is the history. You can read it with git log, compare two points with git diff, and move your files back to any point in it.

Git also lets the chain split and join. A branch is a movable label that points at a commit, so two people can work on two features at once without touching each other’s files. You will build this up over the next lessons in this track.

Commits are snapshots, not file copies#

Git does not copy your whole project on every commit. It stores each distinct file version once and reuses it, so a repository with thousands of commits is usually small. You do not need to manage this. What matters is that every commit can be reconstructed completely.

Local first, shared second#

You run git commit on your own machine, with no network. Sharing is a separate step: you push commits to a remote repository on a host such as GitHub, GitLab, Gitea or a bare repository on your own server. Other machines, including your servers, fetch from that remote. The lesson on remotes, push, pull and fetch covers it in detail.

Why servers use Git#

Before Git, a typical deploy was scp or an FTP upload of files from a laptop. That works until it does not. Someone uploads a file with a local edit nobody committed, forgets one file, or overwrites a newer change. Nobody can say afterwards which version is live.

Pulling from Git fixes four problems:

  • Reproducible deploys. The server runs one named commit, and the same commit gives the same files every time.
  • Fast rollback. Every earlier version still exists in history, so going back means checking out an older commit or tag. The rollbacks lesson shows how this fits into a real release.
  • A reviewable record. git log says who changed what and when, and the commit message says why.
  • Automation. A push to a branch can trigger a build and a deploy, which is the idea behind CI and CD.

A server rarely needs the whole workflow. Often it needs one thing: read access to the repository, so it can run git clone once and git pull or git fetch plus git checkout on every release. You set that up with a read-only deploy key.

What Git does not do#

Git is built for text files such as source code, config and docs. It handles large binary files and database dumps poorly, because each version of a big file is stored in full. Keep uploads, backups and generated build output out of the repository (see .gitignore and secrets).

Git also does not store your secrets safely. Anyone who can read the repository can read every file in its history, including files you deleted later. Passwords and API keys belong in environment variables or a secrets store, not in commits.

Finally, Git is not a backup. A repository on one disk is as safe as that disk. A remote copy helps, but a real backup plan still covers your data.

A rollback in a Git deploy means going back to an older commit, and servers authenticate with an SSH key.

Check it worked#

You do not need to install anything to follow this lesson, but you can see whether Git is already on a server:

bash
git --version

If it prints something like git version 2.43.0, Git is installed. If the shell says command not found, the next lesson installs it. Once you have a repository, git log --oneline lists its commits, newest first, each with a short hash and a message.

Edge cases and gotchas#

  • Git is not GitHub. Git is the tool on your machine. GitHub, GitLab and Gitea are hosts. You can use Git with no host at all, or with a plain SSH server.
  • A commit is permanent in practice. Even if you delete a file in a later commit, the old version stays in history. Treat anything you commit as published.
  • Short hashes are enough. You can usually write the first 7 characters of a hash. Git accepts any unique prefix.
  • Staged is not committed. Changes you added with git add are not in history until you run git commit. Nothing is saved to history before that.
  • Each clone has full history. A fresh git clone downloads every commit by default. On a server with a very large repository you can ask for less with git clone --depth 1, which fetches only the latest snapshot.
  • Commit identity is self-declared. The name and email on a commit come from your config, and Git does not check them. Signed commits and host-side rules exist for teams that need proof.

Troubleshooting#

SymptomLikely causeFix
git: command not foundGit is not installed on this machineInstall it: sudo apt update && sudo apt install git
fatal: not a git repository (or any of the parent directories): .gitYou are in a folder with no repositorycd into the project, or run git init to create one
git log shows nothing and says fatal: your current branch 'main' does not have any commits yetThe repository is new and nothing is committedMake your first commit (next lessons)
fatal: detected dubious ownership in repository at '/srv/app'The folder is owned by another user than the one running GitFix ownership with chown, or if you trust it, git config --global --add safe.directory /srv/app
The server runs old code after a deployFiles were copied by hand, or git pull was not runPull the intended commit and check git rev-parse HEAD on the server
error: Your local changes to the following files would be overwritten by mergeSomeone edited tracked files directly on the serverStop editing on the server, commit changes elsewhere, and reset the server copy deliberately

Quick reference#

bash
git --version              # is Git installed
git log --oneline          # list commits, newest first
git show a1b2c3d           # show one commit
git rev-parse HEAD         # print the full hash you are on
git clone <url>            # download a repository with its history

Read more on the blog: Roll back a failed Docker deploy shows the same idea applied to container images.

Next#

Continue with install and configure Git to put Git on your server and set your name and email.

Check what you learned4 questions
1What does a Git commit record?
2Why is deploying a specific commit more reliable than copying files with scp?
3What is the difference between Git and GitHub?
4How do you roll back a bad deploy when you deploy from Git?

Frequently asked questions

What is Git used for?

Git tracks changes to files over time. Developers use it to keep a history of their code, work on features in parallel, and share code through a hosted remote such as GitHub or GitLab.

What is the difference between Git and GitHub?

Git is the version control tool that runs on your machine. GitHub is a website that hosts Git repositories and adds pull requests, issues and automation on top.

Why do deploy tools pull from Git?

A Git commit identifies one exact version of the code. Pulling from Git means every deploy uses a known, reviewable version, and rolling back means deploying an older commit.

Do I need Git to run a server?

No, you can copy files by hand. Git is the common way to avoid mistakes in that process, because the server gets exactly what was committed and nothing else.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios