Lesson 1 of 8 · 11 min · Foundations of CI/CD
Git workflows that pipelines depend on
What you will learn
- Commits, branches and the commit graph
- Merge versus rebase, conflicts, and undoing things safely (revert, reset, reflog)
- Pull requests, code review and protected branches
- Tags and versioning
- Trunk-based development and Git flow, and why the choice matters for CI/CD
In simple words
Git is the save history of a project: every change, who made it and why. Teams work in separate copies called branches and then combine them. Automatic pipelines start from those events, so the way a team uses Git decides how its software gets tested and released.
Words to know
- Commit: one saved change, with a message that explains it.
- Branch: a separate line of work, so you can change things without disturbing others.
- Merge: combining the work from one branch into another.
- Pull request: asking teammates to review your branch before it is merged.
Why Git is the centre of CI/CD
A pipeline starts when code changes: a push, a pull request, a tag. Git is the source of truth for what is built, and the commit id is the identity of every build, image and release. If you understand Git well, you understand what a pipeline run is testing and how to trace any running version back to its source.
The model
A Git commit is a snapshot of the project plus a message, an author, and a pointer to its parent commit(s). Commits form a graph. A branch is just a movable name pointing at a commit; HEAD points at the branch you are on. Creating a branch is nearly free.
git init -b main
git add app.txt && git commit -m "Add app"
git switch -c feature/login # create and switch to a branch
git commit -am "Add login"
git switch main
git commit -am "Hotfix"
git log --oneline --graph --all
* fb2ec0f Add login
| * edaa6a9 Hotfix
|/
* 21ed06e Add app
The commit ids (hashes) are content-based, so they are unique. The short form (7 to 8 characters) is what you see in pipelines and image tags. The three areas of Git: the working tree (your files), the staging area (what git add prepared) and the repository (commits). git status shows differences between them; git diff shows changes.
Combining work: merge and rebase
Merge joins two lines of history with a merge commit:
git switch main
git merge feature/login
* fc51f91 Merge branch 'feature/login'
|\
| * fb2ec0f Add login
* | edaa6a9 Hotfix
|/
History records exactly what happened, including the parallel work.
Rebase replays your commits on top of another branch, giving a straight line:
git switch feature/search
git rebase main
* ecc4ea3 Add search
* 4d82c52 Main moves on
* 916abcf Merge branch 'feature/login'
Your "Add search" commit was re-created after the newest main commit (it has a new hash). The history is linear and easy to read, but rebase rewrites commits. The golden rule: never rebase or force-push commits that others already have (shared branches such as main). Rebase your own local or feature branch before merging; use merge for shared history. Many teams use "squash and merge" in pull requests: all of a branch's commits become one commit on main.
Conflicts
When two branches change the same lines, Git cannot decide and stops:
CONFLICT (add/add): Merge conflict in c.txt
Automatic merge failed; fix conflicts and then commit the result.
The file contains markers:
<<<<<<< HEAD
line from other
=======
line from main
>>>>>>> main
Edit the file to the intended content (remove the markers), git add it, and git commit (or git rebase --continue). git merge --abort returns to the state before the merge. Prevent painful conflicts with short-lived branches, frequent integration from main and small pull requests.
Undoing things
| Situation | Command | Effect |
|---|---|---|
| Throw away uncommitted edits to a file | git restore file |
gone permanently |
| Park work for later | git stash / git stash pop |
saves and restores local changes |
| Undo a pushed commit | git revert <sha> |
creates a new commit that reverses it; history is preserved, safe on shared branches |
| Move a local branch back | git reset --hard <sha> |
rewrites the branch pointer and discards later commits. Only for unpushed work |
| "I lost a commit!" | git reflog |
shows where HEAD has been; find the old hash and git switch -c rescue <hash> |
Our test: after committing a "bad change", git revert --no-edit HEAD produced Revert "bad change" on top, leaving both commits in the log. Because a revert is just another commit, a pipeline deploys it like any other change. That is the standard way to roll back code (see the rollbacks lesson).
Remotes, pull requests and review
A remote (GitHub, GitLab, Bitbucket) is a shared copy of the repository: git push, git pull (fetch + merge), git fetch, git clone. Teams work through pull requests (PRs; merge requests on GitLab): a branch is pushed, a PR is opened, automated checks run (CI), teammates review, and it is merged when approved and green. Configure branch protection on main: require PRs, required reviews, required passing status checks, no force-push, linear history if you want it. This is where CI becomes a quality gate, not just a build.
Good habits: small PRs, clear messages (what and why), commit often locally, never commit secrets or build output (.gitignore), and sign commits if your organisation requires it.
Branching models
- Trunk-based development: everyone integrates into
main(the trunk) at least daily, via very short-lived branches or directly; incomplete features are hidden behind feature flags (later lesson).mainis always releasable. This is the model that fits continuous delivery best, and the one high-performing teams measured in the DORA research tend to use. - GitHub flow: feature branch → pull request → merge to
main→ deploy. Simple and common. - Git flow: long-lived
develop,release/*,hotfix/*branches. Suits software with scheduled releases and multiple supported versions; it adds merging overhead and delays integration, which makes continuous delivery harder.
The longer branches live, the harder merging becomes ("merge hell") and the later you learn that something is broken. CI exists to reduce exactly that delay.
Monorepo or many repositories
One repository per service gives clear ownership and independent pipelines. A monorepo shares code and tooling, enables atomic cross-service changes and needs smart pipelines that build only what changed (path filters). Neither is wrong; know the trade-off.
Common mistakes
- Rebasing or force-pushing shared branches.
- Long-lived branches that diverge for weeks.
- Committing secrets, large binaries or
node_modules. Removing them later does not erase them from history. git add .blindly, adding build artefacts and.envfiles.- Vague messages like "fix" that make
git loguseless during an incident. - Using
reset --hardon pushed commits instead ofrevert.
Try it yourself
- Create a repository, make two branches that edit the same line, merge, resolve the conflict, and view the graph.
- Repeat with
rebaseand comparegit log --graph. - Make a bad commit, push it to a local bare remote, and undo it with
git revert. - Tag a release, add two commits, and run
git describe --tags.
Check yourself
What is the difference between git merge and git rebase?
Merge combines histories with a merge commit and preserves the real history. Rebase replays your commits on top of another branch, creating new commits for a linear history. Never rebase commits that others already use.
How do you undo a commit that was already pushed to main?
git revert <sha>, which adds a new commit that reverses the change. Rewriting shared history with reset and force-push breaks other people's clones.
What is a tag, and how do pipelines use it?
A permanent name for one commit, typically a release version. Pipelines trigger release builds on tags and use git describe or the tag to name artifacts.
Why does trunk-based development suit CI/CD?
Frequent integration into a single always-releasable branch means problems are found within hours, merges are small, and every commit can flow through the pipeline toward production.
Interview questions
Explain how you would structure Git and branch protection for a team of ten shipping daily.
Trunk-based or GitHub flow with short-lived feature branches, pull requests with required reviews and required CI status checks, protected main without force-push, squash or rebase merges for a clean history, tags for releases, and feature flags for unfinished work.
You accidentally deleted a branch with unmerged work. Can you recover it?
Usually yes: git reflog shows the last commit of that branch; create a branch at that hash with git switch -c recovered <hash>. Objects are kept for a while before garbage collection.
What is a detached HEAD?
HEAD points directly at a commit instead of a branch, for example after checking out a tag or a commit id. New commits there are not on any branch and can be lost unless you create a branch.
Summary
- Git commits form a graph; branches and tags are names for commits; the commit id identifies every build.
- Merge preserves history, rebase rewrites it (not on shared branches), revert safely undoes pushed commits.
- Pull requests with protected branches and required checks make CI a quality gate.
- Trunk-based development with short branches and feature flags fits continuous delivery best.
Going deeper
- Pro Git (the Git book): the complete free reference by the Git project.
- Git reference manual: every command and option.
- GitHub: about protected branches: required reviews and status checks.
- Semantic Versioning 2.0.0: the versioning rules.
- DORA: trunk-based development capability: research-backed practices.
See how much of this you can explain
A free mock interview asks you new questions on CI/CD pipelines and releases for DevOps Engineer and scores every answer, with what to fix.