Git for DevOps Engineers: Essential Guide 2026

Git for DevOps Engineers: Essential Guide

Introduction

Git is the first DevOps skill, and it is the one every other DevOps skill stands on. The pipeline pulls from Git, the deployment is triggered by Git, the infrastructure code lives in Git, and the incident is investigated by reading Git history. A DevOps engineer who is fluent in Git can trace any change to the person, the commit and the reason; one who is not fluent treats the repository as a place where code is stored, rather than as the record of everything the team has done.

This guide covers Git as a DevOps engineer actually uses it: the model underneath the commands, the daily workflow, branching strategies, merging and rebasing, undoing mistakes safely, and how Git fits into CI/CD. It assumes you have used Git at least a little — cloned a repository, made a commit — and builds from there to the level where you can run a team’s Git workflow with confidence. Every command shown here is a real command with correct flags, and the version note below tells you what changed most recently.

Version note (29 Nov 2026): the current Git release line is 2.56, released 28 September 2026, and the latest patch is 2.56.1, released 10 November 2026 as a security release fixing six vulnerabilities, including a code-execution flaw in recursive clones (CVE-2026-111760). Maintenance releases for older lines shipped the same day: 2.55.1, 2.54.1, 2.53.1, 2.52.1, 2.51.3 and 2.50.2. Check your version with git –version and update before cloning repositories you do not control — the recursive-clone flaw is exactly the kind of thing that bites a CI runner, which clones repositories all day.

Why Git Is the First DevOps Skill

Git is a distributed version control system, created by Linus Torvalds in 2005 for the Linux kernel, and it is now the version control system for almost all software and infrastructure work. “Distributed” is the word that matters: every clone is a complete copy of the repository — every commit, every branch, every tag — so any developer’s laptop holds the full history, and the central server is a convention for sharing, not a single point of failure. When the central server is down, the team can still commit, branch and review; when a laptop is lost, nothing is lost, because the history exists in every other clone.

For a DevOps engineer, Git’s value is not just that it stores code. It is that Git makes four things possible, and those four things are the foundation of modern delivery. First, traceability: every change is a commit with an author, a timestamp, a message and a diff, so any line of code — or any line of infrastructure configuration — can be traced to the change that introduced it and the reason given at the time. Second, review: changes travel as pull requests, so nothing reaches the main branch without another person looking at it and a pipeline verifying it. Third, rollback: because every state of the system is a commit, a bad deployment can be undone by returning to the previous commit — the version control system is the rollback mechanism. Fourth, automation: the pipeline is triggered by Git events — a push, a pull request, a tag — so the repository is the control plane for delivery, not just a store.

The Model — Repositories, Commits, Branches and the Three Trees

Git is confusing until you understand its model, and obvious afterwards. The model has three ideas: a commit is a snapshot, a branch is a pointer, and your working directory has three states.

A commit is a snapshot of the whole repository at a moment in time, plus metadata: the author, the timestamp, a message, and a pointer to the previous commit (or commits). Commits form a chain — each one points to its parent — and that chain is the history. A commit is identified by a hash, a long hexadecimal string computed from its contents; the hash is how Git knows whether anything has changed, and why two repositories with the same commits are identical. The message is part of the commit, and it matters: a good message explains why the change was made, not what it does — the diff already shows what.

A branch is a lightweight pointer to a commit. Creating a branch does not copy any files; it creates a pointer, which is why branching in Git is instant and cheap, unlike in older version control systems. The current branch is tracked by a pointer called HEAD. When you commit, Git creates a new commit whose parent is the commit your branch points to, and moves the branch pointer forward. That is all a branch is: a movable label on a commit. A tag is similar, but it does not move — it marks a commit permanently, which is why tags are used for releases (v1.2.0) rather than for work in progress.

The three trees are the states your files can be in. The working directory is the files on disk, as you are editing them. The staging area (also called the index) is the set of changes you have marked for the next commit, with git add. The repository (the .git directory) is the committed history. The daily workflow moves changes through those three states: edit in the working directory, stage with git add, commit with git commit. The commands that confuse beginners are the ones that move changes between these states in the other direction — git restore moves a file from the repository back to the working directory, git reset moves the branch pointer, and git checkout moves HEAD. Keep the three trees in mind and those commands stop being mysterious.

git --version
git status
git log --oneline -10
git log --graph --oneline --all -15

git status is the command to run when you are lost: it shows which branch you are on, which files are staged, modified or untracked, and what Git suggests you do next. git log –oneline shows history compactly; add –graph –all to see branches and merges as a diagram, which is the fastest way to understand what a repository actually looks like.

The Daily Workflow — Clone, Branch, Commit, Push, Review

1. Get the code.

git clone https://github.com/example-org/infrastructure.git
cd infrastructure
git status

Cloning copies the whole repository, including its history, and sets up origin as the remote. Never clone with credentials in the URL, and never clone a repository you do not trust without checking what is in it — a repository can contain hooks and configuration that run code on your machine, which is why the November 2026 security release mattered for CI runners in particular.

2. Work on a branch, never on main.

git checkout -b add-vpc-module
git branch
git branch -a

git checkout -b creates a branch and switches to it in one step. Branch names should say what the branch does: add-vpc-module, fix-alb-health-check, upgrade-terraform-1-17. A branch that lives for hours or a day is healthy; a branch that lives for three weeks is a merge conflict waiting to happen. Check which branches exist with git branch (local) and git branch -a (including remote branches).

3. Make small, coherent commits.

git add modules/vpc/
git add -p
git commit -m "Add VPC module with three availability zones

Creates the VPC, public and private subnets, and the NAT
gateway. Private subnets have no direct internet route."
git log --oneline -3

Commit early and commit often, and make each commit one logical change. A commit that adds a VPC module and also refactors the monitoring configuration is two commits pretending to be one, and it cannot be reverted or reviewed cleanly. git add -p stages changes interactively, hunk by hunk, which is how you split a file’s changes across two commits. The commit message has a subject line (under 50 characters, imperative mood — “Add”, not “Added”) and, for anything non-trivial, a body that explains why. The body is what the engineer debugging this change at 2 a.m. will read, and it is the cheapest documentation you will ever write.

4. Keep the branch up to date, then push.

git fetch origin
git rebase origin/main
git push -u origin add-vpc-module

Fetch first, then integrate the latest main into your branch — rebase is the usual choice for a feature branch, because it keeps history linear (see the merging section below). Push with -u the first time to set the upstream, so later pushes are just git push. If the push is rejected because the remote has commits you do not have, fetch and integrate first; never force-push a shared branch.

5. Open a pull request and let the pipeline verify it.

The pull request is where review happens: another engineer reads the diff, the pipeline runs the build and the tests, and policy checks run — Terraform plan output, linting, secret scanning. Our CI/CD Pipeline Explained covers what the pipeline does at this point, and our GitHub Actions Tutorial covers the workflow that runs it. The rule is absolute: nothing merges to main without a green pipeline and at least one review. Branch protection in the repository settings enforces it — configure it once, and the rule holds even when someone is in a hurry.

6. Merge, then clean up.

git checkout main
git pull
git branch -d add-vpc-module
git push origin --delete add-vpc-module

Branching Strategies — Trunk-Based, GitHub Flow and GitFlow

A branching strategy is a team’s agreement about how branches are used, and it decides how often code is integrated — which is what continuous integration exists to support. There are three strategies that matter, and the choice between them is the most argued-about decision in version control.

Trunk-based development is the strategy CI was designed for. There is one long-lived branch, usually main, and developers work on short-lived branches — hours or a day or two at most — that merge back constantly. Feature flags hide unfinished work from users instead of long-lived branches hiding it from other developers. The pipeline runs on every merge, and because merges are small and frequent, a failure is always attributable to a small change. This is the strategy most teams should use, and it is the only one that works well with continuous deployment. It demands discipline — small changes, flags for incomplete work, and a pipeline fast enough that merging is painless — but it is the strategy that scales.

GitHub Flow is trunk-based development with a pull request at the centre: branch from main, commit, open a pull request, review, merge, deploy. It is the same idea with the review step made explicit, and it is the default for most teams on GitHub. If your team has no branching strategy written down, GitHub Flow is the one to write down — it is simple enough to follow and strict enough to work.

GitFlow is the older alternative: long-lived develop and main branches, plus feature, release and hotfix branches with strict rules about what merges where. It fits teams with scheduled releases and a formal release process — a product that ships every quarter, for example — but it fights CI: long-lived branches mean big, infrequent merges, and big merges are where integration problems hide. It also doubles the places a change must be merged, which doubles the opportunities for a change to be lost. Unless your release process genuinely requires it, prefer trunk-based development.

Whichever strategy you choose, three rules apply to all of them. First, never commit directly to the branch that deploys to production — every change goes through a pull request and the pipeline. Second, keep branches short-lived: a branch that lives for three weeks is a merge conflict waiting to happen. Third, protect the main branch: require the pipeline to pass before a merge is allowed, and require at least one review. Branch protection is configured in the repository settings, not in the pipeline file, and it is what turns “the pipeline must pass” from a convention into a rule.

Merging, Rebasing and Resolving Conflicts

Every branch ends the same way: its changes are integrated into another branch. Git offers two ways to do it — merging and rebasing — and the choice between them is the second most argued-about decision in version control, after the branching strategy itself.

Merging creates a new commit — a merge commit — with two parents: the branch you were on and the branch you merged in. The history preserves the fact that the work happened on a branch and was merged at a particular moment. The command is simple:

git checkout main
git merge add-vpc-module

Merging is safe and honest: nothing is rewritten, and the history shows exactly what happened. Its cost is a history full of merge commits, which can make the log harder to read on a busy repository — though git log –graph makes the structure visible, and many teams accept the noise as the price of an accurate record.

Rebasing replays your branch’s commits on top of another branch, one by one, as if you had started from the newer commit. The result is a linear history with no merge commits:

git checkout add-vpc-module
git rebase main

Rebasing makes history clean and easy to read, and it is the standard way to keep a feature branch up to date with main before merging. Its cost is that it rewrites history: the rebased commits have new hashes, because their parent has changed. That is harmless for a branch only you use, and dangerous for a branch anyone else has pulled — which is why the rule is absolute: never rebase a branch that other people have based work on. Rebase your own feature branch freely; never rebase main, and never rebase a shared branch.

Conflicts happen when two branches change the same lines, or when one branch deletes a file the other modified. Git stops and marks the conflict in the file:

git status
git diff

Open the file, find the conflict markers (<<<<<<<, =======, >>>>>>>), decide what the correct result is — usually a combination of both changes, not a choice between them — remove the markers, stage the file with git add, and continue the merge or rebase. The most common mistake is resolving a conflict by picking one side wholesale, which silently discards the other side’s work. Read both sides, understand both changes, and combine them. If the conflict is in a generated file — a lockfile, for example — regenerating it is usually the right resolution rather than hand-editing it. And if a rebase goes wrong halfway through, git rebase –abort returns you to the state before the rebase started; nothing is lost until you continue past a bad resolution.

Undoing Things Safely — Revert, Reset, Reflog and Stash

Every Git user eventually needs to undo something, and Git offers several ways — with very different safety profiles. Knowing which one to use is the difference between fixing a mistake and making a bigger one.

git revert creates a new commit that undoes a previous commit. It is the safe way to undo anything that has already been pushed and shared, because it does not rewrite history — it adds history. If a bad change reached main, revert it:

git revert abc1234
git log --oneline -3

The revert is itself a commit, which means it goes through the same pull request and pipeline as any other change, and it can itself be reverted if the original change turns out to have been correct after all. For shared history, revert is the only undo you should use.

git reset moves the branch pointer, and it comes in three strengths. git reset –soft moves the pointer but keeps the changes staged. git reset –mixed (the default) moves the pointer and unstages the changes, keeping them in the working directory. git reset –hard moves the pointer and discards the changes entirely. The first two are useful for fixing a local mistake — uncommitting the last commit to split it into two, for example:

git reset --soft HEAD~1
git reset --mixed HEAD~1

–hard is the dangerous one: it destroys uncommitted work, and there is no undo for a –hard reset except the reflog (below). Never run git reset –hard on a branch with work you have not committed somewhere, and never run any form of reset on a branch that has been pushed and shared — resetting shared history is how teams lose commits.

git reflog is the safety net underneath all of it. The reflog records every move HEAD has made — every commit, checkout, reset and rebase — for about 90 days. If you reset the wrong branch, or lose a commit, the reflog shows you where it was:

git reflog
git checkout -b recovery abc1234

Almost every “I lost my work in Git” story ends with the reflog, because Git rarely deletes anything immediately — it just stops pointing at it. Learn git reflog before you need it, not after.

git stash shelves uncommitted changes so you can switch branches or pull without committing half-finished work:

git stash
git stash push -m "WIP: VPC module tests"
git stash list
git stash pop

Stash, switch branches, do the urgent thing, come back, and git stash pop restores the changes. Stashes are easy to forget — git stash list shows them, and a stash with a message is a stash you will find again. For anything that will wait more than a day, commit it to a branch instead of stashing it: a commit is findable, a stash is forgettable.

Git in CI/CD and Team Hygiene

Git’s role in DevOps is bigger than the commands: the repository drives the pipeline, and the pipeline’s quality depends on the repository’s hygiene. Get the hygiene right and the pipeline is trustworthy; get it wrong and the pipeline automates your mistakes.

The pipeline is triggered by Git. A push to a branch runs the build and tests; a pull request runs the full verification suite; a tag triggers a release build; a push to main deploys. The workflow file that defines all of this lives in the repository itself, in .github/workflows/ for GitHub Actions, which means a change to the pipeline is a change like any other — reviewed, tested and traceable. Our GitHub Actions Tutorial covers workflow files in depth, and our CI/CD Pipeline Explained covers the stages those workflows run. The key discipline is that the pipeline runs on every change, not just on the ones someone remembers to test: a pipeline that only runs sometimes is a pipeline that cannot be trusted.

Secrets never go in Git. No API keys, no passwords, no private keys, no tokens — not in a configuration file, not in a commit message, not in a comment. Secrets live in the CI system’s secret store, or in a vault, and reach the pipeline as environment variables. If a secret is ever committed, rotating it is the only fix: removing it from the current commit leaves it in the history, and the history lives in every clone. Secret scanning in the pipeline — GitHub’s secret scanning, or a tool like gitleaks in a pre-commit hook — catches the mistake before the push, which is the only point at which it is cheap to fix.

Hooks automate the small checks. A pre-commit hook runs before a commit is created, and a pre-push hook runs before a push. They can run a linter, a formatter, a secret scanner, or a quick test suite — the checks that are cheap locally and expensive in the pipeline. Keep hooks fast: a hook that takes two minutes will be bypassed, and a bypassed hook is worse than no hook, because the team believes the check is running when it is not.

FAQ

What is the difference between git fetch and git pull?

git fetch downloads the remote’s commits without changing your working files, so you can inspect them first. git pull fetches and then merges the remote branch into yours in one step. Fetch first and look, then decide how to integrate — never pull blindly into a working tree with uncommitted changes.

Should I merge or rebase?

Rebase your own feature branch onto main to keep it current, then merge it — often with a squash merge for a clean history. Never rebase a branch that other people have based work on, and never rebase main, because rebasing rewrites history and anyone who has pulled the old history will have a broken copy.

How do I undo a commit that has already been pushed?

Use git revert, which creates a new commit that undoes the change. It does not rewrite history, so it is safe for shared branches. Never use git reset on a branch that has been pushed — resetting shared history can lose other people’s work.

What should I do if I committed a secret?

Rotate the secret immediately — removing it from the commit is not enough, because it remains in the history and in every clone. Then remove it from the history with a tool like git filter-repo, force-push the cleaned history, and tell anyone who cloned the repository to re-clone. Prevention is better: use secret scanning in a pre-commit hook and keep secrets out of the repository entirely.

Do I need to learn the command line, or is a Git GUI enough?

Learn the command line. A GUI is fine for browsing history and resolving simple conflicts, but the command line is what CI runners, servers and automation use, and it is the only interface that works everywhere — including over SSH on a server at 2 a.m. Learn the commands in this guide, then use a GUI on top of them if you like.

Conclusion

Git is the first DevOps skill because every other DevOps skill depends on it. The model is simple once you see it — commits as snapshots, branches as pointers, three states for your files — and the daily workflow is a loop you will repeat thousands of times: branch, commit, push, review, merge. Branching strategy and the merge-versus-rebase choice are team decisions, not personal preferences, and the undo commands have a clear safety order: revert for shared history, reset only for local mistakes, and the reflog as the safety net underneath it all.

The hygiene is what makes Git a DevOps tool rather than a developer tool: secrets never in the repository, a .gitignore that is correct, hooks that run the cheap checks, signed commits for infrastructure, and a pipeline triggered by every Git event. Build those habits now, while the repository is small, because they are ten times harder to add later. Then go deeper: our GitHub Actions Tutorial for the pipeline Git drives, our CI/CD Pipeline Explained for the stages that pipeline runs, and our Bash Scripting for DevOps for the glue that ties Git, the pipeline and the server together.

Sources checked 29 Nov 2026: Git project security release, 10 Nov 2026, via GitHub announcement (Git 2.56.1 and maintenance releases 2.55.1, 2.54.1, 2.53.1, 2.52.1, 2.51.3, 2.50.2; six vulnerabilities including CVE-2026-111760, code execution during a recursive clone; 2.56 released 28 Sep 2026); Git documentation at git-scm.com (commit, branch, tag, three trees, fetch/pull/push, merge, rebase, revert, reset, reflog, stash, restore, hooks, signing, LFS — command syntax and flags as shown above). NOT VERIFIED on an official page and therefore not stated as fact above: the exact number of commits or contributors in Git 2.56 (not stated above); any claim about Git adoption percentages (not stated above). External references: Git documentation · Git source repository.

—

Scroll to Top