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.
- Updated Nov 27, 2026
- Markdown
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.
- 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#
git switch main
git pull --ff-only
git switch -c fix-login-redirectUpdate 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#
git push -u origin fix-login-redirectThe 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:
cd /srv/app
git fetch origin
git checkout --detach origin/main
docker compose up -d --buildFetching 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#
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:
cd /srv/app
git fetch --tags origin
git checkout --detach v1.2.0
docker compose up -d --buildThe 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:
cd /srv/app
git rev-parse --short HEAD
git describe --tags --alwaysgit 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.0you 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#
| Symptom | Likely cause | Fix |
|---|---|---|
! [remote rejected] main -> main (protected branch hook declined) | Direct pushes to main are blocked | Open a pull request from a branch |
fatal: tag 'v1.2.0' already exists | The name is taken | Pick 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 git | The server has not fetched the tag | git fetch --tags origin |
error: src refspec v1.2.0 does not match any | Pushed a tag that does not exist locally | Create it with git tag -a first |
You are in 'detached HEAD' state. | You checked out a tag or commit | Expected for deploys. Make a branch with git switch -c to work |
error: Your local changes to the following files would be overwritten by checkout | Hand edits on the server | Save them elsewhere, then git restore . deliberately |
fatal: Not possible to fast-forward, aborting. | The server copy diverged from origin | Do not merge on production, use git fetch and git checkout --detach |
| CI does not run on the pull request | No workflow file, or it triggers on other events | Check the pipeline configuration for pull request triggers |
Quick reference#
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 --alwaysRelated lessons#
- Git branches and merging: the branch mechanics used here.
- Git remotes, push, pull and fetch: how branches and tags reach the host.
- What is CI/CD: the automation that reacts to your merge.
- Rollbacks: rolling back safely, including data.
- Zero-downtime deploys: shipping without dropping requests.
Next#
With code flowing to your server, learn where everything lives on it in Linux filesystem layout.
Check what you learned
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
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.