# 10 Git Habits That Make Deployments Less Painful

Most deployment disasters don't start at deploy time. They start weeks earlier, with a 3,000-line pull request, a vague commit message, or a branch that drifted away from `main` and never came back.

The good news is that a handful of small Git habits can make releases boring, and boring is exactly what you want from a deployment. Here are ten worth building into your team's routine.

* * *

## 1\. Keep `main` always deployable

If `main` is broken, every deployment, hotfix, and rollback becomes harder. Treat it as the source of truth for what could ship right now.

-   Never push half-finished work directly to `main`.
    
-   Run your tests and linters on every change before it merges.
    
-   If something breaks on `main`, fixing or reverting it is the team's top priority.
    

When `main` is always releasable, deploying becomes a choice you make, not a project you plan.

## 2\. Make small, atomic commits

An atomic commit does one logical thing: fixes one bug, renames one function, adds one endpoint. Small commits pay off during deployment trouble:

-   `git revert` can undo exactly the change that caused the problem, without dragging unrelated work along.
    
-   `git bisect` can pinpoint the offending commit quickly.
    
-   Reviewers can actually understand what they're approving.
    

```bash
# Stage only the parts of a file that belong to this commit
git add -p
```

If your commit message needs the word "and", it's probably two commits.

## 3\. Write commit messages for your future self

At 2 a.m., during an incident, you will be reading `git log`. Make it useful.

A good message has a short summary line (about 50 characters), a blank line, and a body explaining **why** the change was made, not just what changed.

```text
fix(auth): handle expired refresh tokens

Users were being logged out with a 500 error when their refresh
token expired during an active session. Return a 401 and let the
client redirect to the login page instead.

Fixes #482
```

Adopting a convention such as [Conventional Commits](https://www.conventionalcommits.org/) (`feat:`, `fix:`, `chore:`) also lets you generate changelogs and version bumps automatically.

## 4\. Keep branches short-lived

Long-running branches are the biggest source of painful merges. The longer a branch lives, the more it diverges, and the more likely a deploy-day merge turns into a conflict-resolution marathon.

-   Aim for branches that live a few days, not a few weeks.
    
-   Break large features into smaller pieces that can merge independently.
    
-   Hide unfinished work behind **feature flags** so it can be merged and deployed without being visible to users.
    

Feature flags separate _deploying_ code from _releasing_ it, which removes a lot of pressure from release day.

## 5\. Sync with `main` often

Don't wait until your pull request is "done" to discover that `main` moved underneath you. Pull in changes regularly so conflicts stay small and fresh in your mind.

```bash
# Update your branch with the latest main
git fetch origin
git rebase origin/main
```

Many teams also set `git pull` to rebase by default to avoid noisy merge commits:

```bash
git config --global pull.rebase true
```

Whether you prefer rebasing or merging, the habit that matters is doing it **often**.

## 6\. Protect `main` and require reviews and CI

Good habits are easier to keep when the tooling enforces them. On GitHub, GitLab, or Bitbucket, enable branch protection on `main`:

-   Require pull requests instead of direct pushes.
    
-   Require at least one approving review.
    
-   Require status checks (tests, build, lint) to pass before merging.
    
-   Block force pushes and branch deletion.
    

This turns "please don't break main" from a request into a guarantee.

## 7\. Tag every release

A deployment should point at something specific and immutable. Branches move; tags don't. Tag each release so you always know exactly what's running in production.

```bash
git tag -a v1.4.0 -m "Release 1.4.0"
git push origin v1.4.0
```

Use [semantic versioning](https://semver.org/) (`MAJOR.MINOR.PATCH`) so the version number communicates the risk of an upgrade. Deploy from tags, and rolling back becomes as simple as deploying the previous one.

## 8\. Keep secrets and generated files out of the repo

Committing an API key is one of the most common and costly Git mistakes. Once a secret is in history, deleting the file isn't enough.

-   Add `.env`, credentials, and build artifacts to `.gitignore` from day one.
    
-   Use a secrets manager or your platform's environment variables for real values.
    
-   Add a pre-commit hook or CI check with a scanner such as `gitleaks` or `trufflehog`.
    
-   If a secret does leak, **rotate it immediately**. Assume it's compromised.
    

```gitignore
.env
*.pem
node_modules/
dist/
```

Keep in mind that `.gitignore` only affects files Git isn't already tracking. If a file was committed before you ignored it, remove it from the index with `git rm --cached <file>`.

## 9\. Don't rewrite shared history; revert instead

Rewriting history on a branch other people use is a reliable way to cause chaos. Once a commit is on `main` or has been deployed, treat it as permanent.

When something goes wrong in production, undo it with a new commit:

```bash
# Undo a regular commit
git revert <commit-sha>

# Undo a merge commit (keep the first parent, usually main)
git revert -m 1 <merge-commit-sha>
```

This keeps the history honest and lets your deployment pipeline roll forward safely.

One gotcha: after reverting a merge, Git considers those changes already handled. If you later want to merge the same branch again, you'll first need to revert the revert, or the original changes won't come back.

On your own feature branches, rewriting is fine, but prefer the safer flag:

```bash
git push --force-with-lease
```

Unlike plain `--force`, it refuses to push if the remote branch has commits you haven't fetched, which protects a teammate's work.

## 10\. Review what's shipping before you ship it

Before every deployment, look at exactly what's about to go out. Git makes this trivial when you tag releases.

```bash
# What commits are in this release?
git log --oneline v1.3.0..v1.4.0

# Which files changed?
git diff --stat v1.3.0..v1.4.0
```

Scanning that list takes two minutes and catches surprises: a migration you forgot about, a config change that needs coordination, or an experimental commit that shouldn't be there.

* * *

## Quick pre-deploy checklist

-   `main` is green: tests and builds pass
    
-   The release is tagged with a clear version
    
-   You've reviewed the commits and diff since the last release
    
-   No secrets or debug code slipped in
    
-   You know how to roll back (previous tag or `git revert`)
    

## Final thoughts

None of these habits are glamorous, and none require new tools. But together they shrink the gap between "it works on my machine" and "it's running safely in production." Start with one or two that address your team's biggest pain points, and make them the default. Deploy day should be the least exciting part of your week.

_What Git habit has saved your team the most pain? Share it in the comments._

---

*Published via [ZyVOP](https://zyvop.com/10-git-habits-that-make-deployments-less-painful-hn58q?utm_source=hashnode&utm_medium=crosspost&utm_campaign=syndication) — Write once in Markdown, auto-backup to GitHub, and syndicate to Dev.to, Medium & Hashnode in 1 click.*
