Git Essentials for Infra Repos
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.
Configure Identity (Once per Machine)
Section titled “Configure Identity (Once per Machine)”git config --global user.name "Your Name"Clone and Status
Section titled “Clone and Status”git clone https://github.com/org/infra.gitcd infragit statusgit remote -vBranches
Section titled “Branches”git switch -c feature/vpc-peering # create and switchgit switch maingit 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.
Commit and Push
Section titled “Commit and Push”git add -p # stage interactively (good habit)git commit -m "fix(aws): correct NAT route"git push -u origin feature/vpc-peeringFollow your team’s commit message convention (e.g. Conventional Commits).
Pull Requests
Section titled “Pull Requests”- Push a feature branch.
- Open a PR against
main(ordevelop). - Request review; CI must pass for infra repos when pipelines are wired.
- Squash or merge per team policy — squashing keeps history linear for small teams.
Undo Local Mistakes
Section titled “Undo Local Mistakes”| Situation | Command |
|---|---|
| Unstage file | git restore --staged path/to/file |
| Discard local edits | git restore path/to/file |
| Amend last commit (not pushed) | git commit --amend |
Revert a Bad Merge on Main
Section titled “Revert a Bad Merge on Main”git switch maingit pullgit revert -m 1 <merge_commit_sha> # -m 1 = mainline parentgit pushPrefer revert on shared branches over reset —hard (rewrites history others may have pulled).
Useful One-Liners
Section titled “Useful One-Liners”git log --oneline -10git show <commit>git diff main...feature/vpc-peeringSummary
Section titled “Summary”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.