TL;DR

Your Git cheat sheet for SRE and DevOps work: branch management, rebasing for clean history, cherry-picking hotfixes, reverting bad commits, and using git bisect to find the commit that introduced a regression.

Daily Commands

These cover 90% of daily Git work — always check git status before committing and use --dry-run on destructive operations.

bashdaily.sh
git status                        # working tree status
git diff                          # unstaged changes
git diff --staged                 # staged changes

git add -p                        # interactively stage chunks (avoids committing debug code)
git commit -m "fix: descriptive message"
git commit --amend --no-edit      # add forgotten changes to the last commit (local only)

git log --oneline -20             # compact history
git log --oneline --graph --all   # branch graph
git log -p -1                     # show the diff of the last commit
git show <sha>                    # show a specific commit

git stash                         # stash all changes
git stash pop                     # restore stash
git stash list
git stash show -p stash@{0}      # diff of a specific stash

Branching

Use short-lived feature branches; keep main/master always deployable; merge via pull request with at least one review.

bashbranching.sh
git checkout -b feature/my-feature main   # create branch from main
git checkout -b hotfix/sev2-fix main

git branch -a                    # list all branches (local + remote)
git branch -d feature/done       # delete merged branch
git branch -D feature/abandoned  # force delete unmerged branch

git push -u origin feature/my-feature   # push and set upstream
git push --delete origin old-branch     # delete remote branch

# Update local branch list after remote deletes
git remote prune origin
git fetch --prune

Rebase

Use rebase to keep feature branches up to date with main (cleaner than merge commits), and interactive rebase to clean up a messy commit history before opening a PR.

bashrebase.sh
# Rebase feature branch onto latest main
git checkout feature/my-feature
git fetch origin
git rebase origin/main

# Resolve conflicts during rebase
git status                        # see conflicted files
# Edit files, then:
git add <resolved-file>
git rebase --continue
# Or abort the rebase:
git rebase --abort

# Interactive rebase: clean up last 5 commits before PR
git rebase -i HEAD~5
# In the editor: pick, squash (s), fixup (f), reword (r), drop (d)

# After cleaning up, force push (only on your own branch — never shared branches)
git push --force-with-lease      # safer than --force: fails if remote changed

Cherry-pick

Cherry-pick applies a specific commit to a different branch — the primary tool for backporting a hotfix from main to a release branch without merging the full history.

bashcherry-pick.sh
# Backport a hotfix commit to the release branch
git log --oneline main | grep "hotfix"    # find the commit sha
git checkout release/1.4
git cherry-pick <commit-sha>              # apply that commit here

# Cherry-pick a range of commits
git cherry-pick <sha1>^..<sha2>          # from sha1 to sha2 inclusive

# If conflicts arise
git cherry-pick --continue               # after resolving
git cherry-pick --abort                  # abandon

# cherry-pick without committing (review first)
git cherry-pick --no-commit <sha>
git diff --staged                        # review what was applied

Revert (Safe Undo)

git revert creates a new commit that undoes a previous commit without rewriting history — the safe way to undo on shared/production branches; use reset only on private local commits.

bashrevert.sh
# Revert the last commit (creates a new "revert" commit)
git revert HEAD

# Revert a specific commit
git revert <sha>

# Revert a merge commit (specify the mainline parent)
git revert -m 1 <merge-commit-sha>

# Revert without auto-committing (to review or combine with other changes)
git revert --no-commit <sha>
git commit -m "revert: undo broken deploy"

# Local-only undo (NEVER use on shared branches)
git reset HEAD~1              # undo last commit, keep changes staged
git reset --soft HEAD~1       # undo last commit, keep changes in working tree
git reset --hard HEAD~1       # undo and DELETE local changes (irreversible)

git bisect (Find Regression)

git bisect binary-searches commit history to find the exact commit that introduced a bug or regression — far faster than manually checking out commits one by one.

bashbisect.sh
git bisect start
git bisect bad                           # current commit has the bug
git bisect good <known-good-sha>         # this commit was fine

# Git checks out the midpoint commit
# Test it: does the bug exist?
git bisect bad                           # yes, bug exists here
# or
git bisect good                          # no, this commit is fine
# Repeat until git identifies the first bad commit

# Skip commits that can't be tested (e.g., build failure)
git bisect skip

# Automate with a test script (0 = good, 1 = bad)
git bisect run ./test.sh

# Always end the session
git bisect reset