Skip to content

Git Essentials for Infra Repos

First PublishedByAtif Alam

This page is a short operator-oriented Git reference for infrastructure-as-code and CI/CD repos. For tags and releases, see Release management. For GitOps, see GitOps. For signed commits, see Compliance.

Terminal window
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Terminal window
git clone https://github.com/org/infra.git
cd infra
git status
git remote -v
Terminal window
git switch -c feature/vpc-peering # create and switch
git switch main
git pull --ff-only origin main # update local main (avoid merge commits on shared branches)

Use --ff-only on automation-friendly workflows so unexpected divergence fails fast instead of creating silent merge commits.

Terminal window
git add -p # stage interactively (good habit)
git commit -m "fix(aws): correct NAT route"
git push -u origin feature/vpc-peering

Follow your team’s commit message convention (e.g. Conventional Commits).

  1. Push a feature branch.
  2. Open a PR against main (or develop).
  3. Request review; CI must pass for infra repos when pipelines are wired.
  4. Squash or merge per team policy — squashing keeps history linear for small teams.
SituationCommand
Unstage filegit restore --staged path/to/file
Discard local editsgit restore path/to/file
Amend last commit (not pushed)git commit --amend
Terminal window
git switch main
git pull
git revert -m 1 <merge_commit_sha> # -m 1 = mainline parent
git push

Prefer revert on shared branches over reset —hard (rewrites history others may have pulled).

Terminal window
git log --oneline -10
git show <commit>
git diff main...feature/vpc-peering

Branch → commit → push → PR → merge is the core loop. Treat main as protected: no direct pushes, required reviews, and Terraform/Ansible plans in CI where possible.