Git Cheatsheet for Cloud Teams

Git is the foundation of every modern cloud deployment pipeline. Knowing it well is not optional — it is the mechanism by which code gets from your laptop to production.

Core Commands#

CommandWhat it does
git initInitialise a new local repository
git clone <url>Copy a remote repository to your machine
git add <file>Stage a file for the next commit
git add -pInteractively stage chunks (review before committing)
git commit -m "message"Record staged changes with a message
git statusShow staged, unstaged, and untracked changes
git logShow commit history
git diffShow unstaged changes
git diff --stagedShow staged changes (what will be committed)
git branchList local branches
git branch -aList local and remote branches
git checkout -b feature/my-featureCreate and switch to a new branch
git switch -c feature/my-featureSame as above (newer syntax)
git merge feature/my-featureMerge a branch into the current branch
git rebase mainReplay commits on top of main
git push origin feature/my-featurePush a branch to the remote
git pullFetch and merge remote changes
git fetchDownload remote changes without merging
git stashSave uncommitted changes temporarily
git tag v1.0.0Create a lightweight tag

Useful Flags and Shortcuts#

# Compact log with branch graph
git log --oneline --graph --all

# Push and set upstream tracking in one step
git push -u origin feature/my-feature

# Amend the last commit message or add forgotten files (local commits only)
git commit --amend

# Apply stash and remove it
git stash pop

# Apply stash but keep it in the stash list
git stash apply

# Stash including untracked files
git stash -u

# Show what changed in a specific commit
git show abc1234

# See which commits touched a specific file
git log --follow -- path/to/file.tf

Branch Workflows#

Feature branch workflow#

This is the most common pattern for teams using GitHub or GitLab.

git checkout main && git pull           # start from the latest main
git checkout -b feature/add-gcs-bucket  # create a feature branch
# ... make changes ...
git add -p                              # stage carefully
git commit -m "feat: add GCS bucket for audit logs"
git push -u origin feature/add-gcs-bucket
# open a pull request / merge request
# after merge:
git checkout main && git pull
git branch -d feature/add-gcs-bucket

Trunk-based development#

Branches are short-lived (hours, not days). Teams commit directly to main or via very short feature branches.

GitOps pattern#

The Git repository is the single source of truth for what runs in production.

Merge vs Rebase#

MergeRebase
HistoryPreserves branch history; creates a merge commitRewrites commits onto the target; linear history
When to useIntegrating long-lived branches; preserving contextCleaning up local commits before a PR
RiskNone — does not rewrite historyNever rebase branches others have checked out
ResultNon-linear history with merge commitsClean linear history

Rule: rebase your own local work before pushing. Never rebase a branch that has already been pushed and shared with others — it rewrites commit hashes and will break their local copies.

Undoing Things#

# Undo the last commit but keep changes staged
git reset --soft HEAD~1

# Undo the last commit and unstage changes (changes kept in working directory)
git reset HEAD~1

# Discard local changes to a file (cannot undo)
git restore path/to/file.tf

# Discard ALL local changes (cannot undo — be sure)
git reset --hard HEAD

# Safely undo a commit that has already been pushed (creates a new commit)
git revert abc1234

# Unstage a file without discarding changes
git restore --staged path/to/file.tf

git reset --hard discards all uncommitted changes permanently. git revert is the safe alternative for undoing pushed commits because it does not rewrite history.

Remote Operations#

# Show configured remotes
git remote -v

# Add a remote
git remote add origin https://github.com/myorg/myrepo.git

# Change a remote URL (e.g., after a repo rename)
git remote set-url origin https://github.com/myorg/new-name.git

# Fetch all remote branches without merging
git fetch --all

# Delete a remote branch
git push origin --delete feature/old-branch

fetch vs pull#

git fetch downloads changes from the remote but does not touch your working directory. git pull is shorthand for git fetch followed by git merge. Use git fetch first when you want to inspect changes before merging.

Conflict Resolution#

When Git cannot auto-merge, it marks conflict zones in the file:

<<<<<<< HEAD
resource "aws_s3_bucket" "logs" {
  bucket = "prod-logs-bucket"
=======
resource "aws_s3_bucket" "logs" {
  bucket = "prod-audit-logs-bucket"
>>>>>>> feature/rename-bucket

Steps to resolve:

  1. Open the conflicted file and decide which version is correct (or combine both)
  2. Remove all conflict markers (<<<<<<<, =======, >>>>>>>)
  3. git add path/to/resolved-file.tf
  4. git commit (or git rebase --continue if you were rebasing)

Use git mergetool to open a visual diff tool if your editor supports it.

Tags#

# Create a lightweight tag (just a pointer to a commit)
git tag v1.0.0

# Create an annotated tag (stores tagger name, date, message)
git tag -a v1.0.0 -m "First stable release"

# Push tags to remote (git push does not push tags by default)
git push origin v1.0.0
git push origin --tags        # push all tags at once

# Delete a local tag
git tag -d v1.0.0

# Delete a remote tag
git push origin --delete v1.0.0

# List all tags
git tag -l

Use annotated tags for releases. They carry metadata and can be signed. Lightweight tags are fine for local bookmarks.

.gitignore Patterns for Cloud Projects#

# Terraform
*.tfstate
*.tfstate.backup
.terraform/
.terraform.lock.hcl
*.tfplan

# Credentials and secrets
.env
.env.local
*.pem
*.key
*credentials*
secrets.yaml

# Cloud provider credential files
.aws/credentials
gcloud/

# Node / Python
node_modules/
__pycache__/
*.pyc
.venv/
dist/
build/

# Editor files
.DS_Store
.idea/
*.swp

Never commit .tfstate — it can contain secrets and causes conflicts when multiple people share it. Use remote state instead.

Git Configuration#

# Identity (required for commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Set the default branch name for new repos
git config --global init.defaultBranch main

# Store credentials (use a credential manager in production)
git config --global credential.helper store

# Set your preferred editor for commit messages
git config --global core.editor "vim"

# Show the config
git config --list

Typical CI/CD Pipeline Triggered by Git Push#

  1. Developer pushes to a feature branch on GitHub/GitLab
  2. CI system (GitHub Actions, GitLab CI, Cloud Build) detects the push
  3. Pipeline runs: lint → unit tests → build → integration tests
  4. On pull request merge to main: pipeline also runs Docker build + push to registry
  5. CD system (Argo CD, Flux, or a deploy step) applies the new image to the cluster
  6. On tag push (v*): pipeline creates a release artifact or triggers a production deploy

The git event (push, PR open, tag create) is the trigger for every step. Getting comfortable with Git is getting comfortable with your entire deployment process.