Chapter 6 · Lesson 10 of 10
Git for deployers

A Git workflow that triggers a deploy

A Git deploy workflow moves a change through a feature branch, pull request and review into main, where CI tests it and a deploy ships it, with tags marking releases you can redeploy.

By the end you will

Run a feature branch to production through a pull request and CI, tag a release with git tag -a, and roll back by redeploying an earlier tag.

Time
20 min
Level
Intermediate
Check
4 questions
You need
  • A repository on a Git host with CI available
  • A server or deploy tool that can pull from the repository

A workable deploy workflow is: branch off main, push the branch, open a pull request, get a review, let CI pass, merge to main, deploy from main, and tag the release. Every step leaves a record, and rolling back is a matter of deploying an older tag. This lesson wires the earlier Git pieces into that loop.

How it works#

The idea is that main is always deployable. Nobody commits to it directly. Changes arrive through short-lived branches and pull requests (some hosts call them merge requests). Between the branch and main sit two gates: a human review and automated checks. After a merge, CI/CD builds and tests the new main, and a deploy step ships it to the server.

This is a simple, widely used shape sometimes called GitHub flow. Larger teams add release branches or staging environments, but the building blocks are the same: branches, merges, tags.

Step 1: Start a feature branch#

bash
git switch main
git pull --ff-only
git switch -c fix-login-redirect

Update main first so you branch from the latest code. Keep the branch small and about one thing. Commit as you go with clear messages, as in your first repository.

Step 2: Push and open a pull request#

bash
git push -u origin fix-login-redirect

The host prints a link to open a pull request. Describe what changed and why, and how you tested it. CI usually starts on its own: tests, lint, and maybe a build of the container image. If you need a refresher on branches, see branches and merging.

Step 3: Review and merge#

A reviewer reads the diff and comments. Fix issues by committing to the same branch, which updates the pull request. When the checks are green and the review is approved, merge it. Hosts offer three styles:

  • Merge commit. Keeps every branch commit plus a merge commit.
  • Squash. Combines the branch into one commit on main, which keeps history short.
  • Rebase and merge. Replays the commits without a merge commit.

Pick one per project and stay with it. Turn on branch protection for main so it requires a pull request and passing checks, and blocks force pushes. If the merge conflicts, see resolving conflicts.

Step 4: Deploy from main#

On a push to main, your pipeline or deploy tool pulls the new commit. A minimal manual version on a server looks like this:

bash
cd /srv/app
git fetch origin
git checkout --detach origin/main
docker compose up -d --build

Fetching and checking out an exact commit means the server runs what was reviewed. Do not edit files on the server, and keep real secrets outside the repository. For releases without downtime, see zero-downtime deploys.

Step 5: Tag the release#

bash
git switch main
git pull --ff-only
git tag -a v1.2.0 -m "Release 1.2.0"
git push origin v1.2.0

-a makes an annotated tag with a message, author and date. git push --tags pushes all your local tags at once, but pushing a named one is more deliberate. A common naming scheme is vMAJOR.MINOR.PATCH. Some pipelines deploy on tag pushes instead of on every merge. To see them, run git tag -n or git show v1.2.0.

Step 6: Roll back by redeploying a tag#

If v1.3.0 misbehaves, deploy the last good tag:

bash
cd /srv/app
git fetch --tags origin
git checkout --detach v1.2.0
docker compose up -d --build

The server now runs the exact code of 1.2.0. This is the quick fix and it changes no history. Then fix the problem on a new branch. If the broken commit should leave main too, use git revert on it (see undo mistakes), never a force push to main. The rollbacks lesson covers data changes and database migrations that do not roll back so neatly. See also the blog post roll back a failed Docker deploy.

For the vocabulary, see CI/CD and rollback in the glossary, and the blog post Deploy an Express app with Docker on a VPS.

Check it worked#

After a merge and deploy, confirm the server runs the commit you expect:

bash
cd /srv/app
git rev-parse --short HEAD
git describe --tags --always

git describe prints the nearest tag, such as v1.2.0, or v1.2.0-3-ga1b2c3d when you are three commits past it. Compare with the host’s view of main. Then check the app responds and the health check passes.

Edge cases and gotchas#

  • Tags are not branches. After git checkout v1.2.0 you are in detached HEAD state. That is fine for deploying. Do not commit there without making a branch.
  • Never move a published tag. Others may have deployed it. Cut a new patch version instead.
  • Fetch tags explicitly. A server that only fetches branches will not see a new tag. Use git fetch --tags.
  • Long branches hurt. The longer a branch lives, the harder the merge and the riskier the release.
  • A green pipeline can still ship a bad release. Add a health check and keep the previous tag handy.
  • Database changes. A rollback of code does not roll back data. Plan migrations to work with both the old and new code.

Troubleshooting#

SymptomLikely causeFix
! [remote rejected] main -> main (protected branch hook declined)Direct pushes to main are blockedOpen a pull request from a branch
fatal: tag 'v1.2.0' already existsThe name is takenPick the next version, or delete only if unpushed with git tag -d v1.2.0
error: pathspec 'v1.2.0' did not match any file(s) known to gitThe server has not fetched the taggit fetch --tags origin
error: src refspec v1.2.0 does not match anyPushed a tag that does not exist locallyCreate it with git tag -a first
You are in 'detached HEAD' state.You checked out a tag or commitExpected for deploys. Make a branch with git switch -c to work
error: Your local changes to the following files would be overwritten by checkoutHand edits on the serverSave them elsewhere, then git restore . deliberately
fatal: Not possible to fast-forward, aborting.The server copy diverged from originDo not merge on production, use git fetch and git checkout --detach
CI does not run on the pull requestNo workflow file, or it triggers on other eventsCheck the pipeline configuration for pull request triggers

Quick reference#

bash
git switch -c fix-login-redirect
git push -u origin fix-login-redirect
# open a pull request, review, CI passes, merge
git switch main && git pull --ff-only
git tag -a v1.2.0 -m "Release 1.2.0"
git push origin v1.2.0
git fetch --tags origin
git checkout --detach v1.2.0       # roll back or deploy a tag
git describe --tags --always

Next#

With code flowing to your server, learn where everything lives on it in Linux filesystem layout.

Check what you learned4 questions
1Why merge through a pull request instead of pushing to main directly?
2Which command creates an annotated tag for release 1.2.0?
3A plain git push does not upload your tags. How do you push them?
4Version 1.3.0 is broken in production. What is the quickest safe rollback?

Frequently asked questions

What is a good Git workflow for deploying?

Work on a short-lived feature branch, open a pull request, get a review and passing checks, merge to main, and deploy from main. Tag each release so you can find and redeploy it.

What is the difference between a branch and a tag?

A branch moves forward as you commit. A tag is a fixed label on one commit, which makes it a stable name for a release.

How do I tag a release in Git?

Run git tag -a v1.2.0 -m "Release 1.2.0" on the commit to release, then git push origin v1.2.0 or git push --tags to share it.

How do I roll back a deploy that uses Git?

Redeploy a previous tag or commit that worked, for example v1.1.0. If the bad change must also leave main, use git revert so history stays intact.

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