# 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.

URL: https://levelrail.com/learn/git/git-workflow-that-triggers-a-deploy/
Updated: 2026-11-27

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](https://levelrail.com/learn/deploy-operate/what-is-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](https://levelrail.com/learn/git/your-first-git-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](https://levelrail.com/learn/git/git-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](https://levelrail.com/learn/git/resolve-git-merge-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](https://levelrail.com/learn/deploy-operate/environment-variables-and-secrets/)) outside the repository. For releases without downtime, see [zero-downtime deploys](https://levelrail.com/learn/deploy-operate/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](https://levelrail.com/learn/git/undo-mistakes-in-git/))), never a force push to `main`. The [rollbacks lesson](https://levelrail.com/learn/deploy-operate/rollbacks/)) covers data changes and database migrations that do not roll back so neatly. See also the blog post [roll back a failed Docker deploy](https://levelrail.com/blog/rollback-failed-deploy-docker/)).

For the vocabulary, see [CI/CD](https://levelrail.com/learn/glossary/#ci-cd)) and [rollback](https://levelrail.com/learn/glossary/#rollback)) in the glossary, and the blog post [Deploy an Express app with Docker on a VPS](https://levelrail.com/blog/deploy-express-app-docker-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](https://levelrail.com/learn/glossary/#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

| 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

```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
```

## Related lessons

- [Git branches and merging](https://levelrail.com/learn/git/git-branches-and-merging/)): the branch mechanics used here.
- [Git remotes, push, pull and fetch](https://levelrail.com/learn/git/git-remotes-push-pull-fetch/)): how branches and tags reach the host.
- [What is CI/CD](https://levelrail.com/learn/deploy-operate/what-is-ci-cd/)): the automation that reacts to your merge.
- [Rollbacks](https://levelrail.com/learn/deploy-operate/rollbacks/)): rolling back safely, including data.
- [Zero-downtime deploys](https://levelrail.com/learn/deploy-operate/zero-downtime-deploys/)): shipping without dropping requests.

## Next

With code flowing to your server, learn where everything lives on it in [Linux filesystem layout](https://levelrail.com/learn/linux/linux-filesystem-layout/)).

## 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

- [git-tag manual](https://git-scm.com/docs/git-tag)
- [Pro Git, tagging](https://git-scm.com/book/en/v2/Git-Basics-Tagging)
