Git and CI/CD
CI/CD stands for continuous integration and continuous delivery (or deployment). Continuous integration means that every change pushed to the repository is automatically built and tested on a server, so problems are found within minutes of the push, not weeks later. Continuous delivery goes further: every change that passes is automatically prepared for release, and with continuous deployment, released. Git is the trigger for all of it: a push, a pull request or a tag starts a pipeline, a list of jobs defined in a file committed to the repository.
A factory production line works this way. Every part that arrives goes through the same automatic checks, measurements and packaging, in the same order. A faulty part is stopped at the first check that fails, long before it reaches a customer.
How a Push Starts a Pipeline
- A developer pushes a commit, or opens or updates a pull request.
- The hosting platform sees the event and reads the pipeline file at that commit.
- A runner (a fresh virtual machine or container) clones the repository at that exact commit.
- The runner executes the jobs: build, tests, code checks, packaging, deployment.
- The result is reported back as a status check on the commit and pull request: a green check mark or a red X.
- Protected-branch rules (see "Git Workflows") can require the check to pass before merging.
| Platform | Pipeline file | Runs on |
|---|---|---|
| GitHub Actions | .github/workflows/<name>.yml | GitHub-hosted or your own runners |
| GitLab CI/CD | .gitlab-ci.yml | GitLab runners (shared or your own) |
| Bitbucket Pipelines | bitbucket-pipelines.yml | Bitbucket's cloud, or your own runners |
A Pipeline for a Java Project
These files build and test a Maven project with Java 21 on every push and pull request. mvn -B verify compiles, runs the tests and fails the pipeline if anything fails (-B is batch mode, without interactive output):
# .github/workflows/build.yml
name: Build
on:
push:
branches: [main]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: "21"
cache: maven
- run: mvn -B verify# .gitlab-ci.yml
image: maven:3.9-eclipse-temurin-21
build:
stage: test
script:
- mvn -B verify
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH# bitbucket-pipelines.yml
image: maven:3.9-eclipse-temurin-21
pipelines:
default:
- step:
name: Build and test
caches:
- maven
script:
- mvn -B verify
pull-requests:
"**":
- step:
name: Build and test the pull request
caches:
- maven
script:
- mvn -B verifyThe version numbers (@v4, 3.9, 21) were current when this lesson was written. Each platform's documentation shows the newest. Deployment jobs are added the same way, usually limited to main or to tags.
Git Inside CI: Shallow Clones
To save time, CI systems often clone shallowly, with only the newest commit (GitHub Actions' actions/checkout fetches a depth of 1 by default) or a limited number of commits. Most builds don't need history, but commands that do, such as git describe, git log and git blame, then fail or give wrong answers. Navika's project has a release tag, and a CI-style clone sees none of it:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
git tag -a v1.0.0 -m "First release"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
cd ..
git clone --quiet --depth 1 "file://$(pwd)/fee-tracker" ci-checkout
cd ci-checkout
git log --oneline
git describe3550432 (grafted, HEAD -> main, origin/main, origin/HEAD) Add the late fee
fatal: No names found, cannot describe anything.Explanation: the shallow clone has only the newest commit (grafted marks the cut), and no tag, so git describe has nothing to describe from. The fix is to fetch the rest when a job needs it:
git fetch --quiet --unshallow --tags
git describev1.0.0-1-g3550432In the pipeline files, the same is done with an option: fetch-depth: 0 for actions/checkout, the GIT_DEPTH variable in GitLab, and clone: depth: full in Bitbucket.
Git Inside CI: Detached HEAD
CI usually checks out the exact commit that triggered the pipeline, not a branch, so HEAD is detached (see "HEAD and Detached HEAD"):
git switch --quiet --detach HEAD
git rev-parse --abbrev-ref HEAD
git branch --show-currentHEADgit rev-parse --abbrev-ref HEAD prints HEAD instead of a branch name, and git branch --show-current prints nothing. Scripts that need the branch name should use the variable the platform provides: GITHUB_REF_NAME on GitHub Actions, CI_COMMIT_REF_NAME on GitLab, and BITBUCKET_BRANCH on Bitbucket.
Scripting Git Safely
Pipelines and scripts must not depend on output meant for people, which can change between Git versions or with the user's settings. Use plumbing commands, --porcelain options and exit codes:
| Need | Safe command |
|---|---|
| Is the working tree clean? | git status --porcelain prints nothing when clean, and its format is stable |
| Did a step change any file? | git diff --exit-code exits with 1 if there are changes |
| Current commit | git rev-parse HEAD (or --short) |
| A version name | git describe --tags --always --dirty |
| List branches or tags | git for-each-ref --format="%(refname:short)" refs/heads |
| Names with unusual characters | Add -z to separate entries with zero bytes instead of new lines |
A common pipeline check: after a code generator or formatter runs, the working tree must not change, otherwise someone forgot to commit the generated files. Here the "formatter" is one shell line that adds a new line at the end of the file if it is missing (tail -c 1 reads the last character):
cd ../fee-tracker
printf 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
[ -n "$(tail -c 1 fees.txt)" ] && echo >> fees.txt
git status --porcelain
git diff --exit-code --stat || echo "Formatting changed files: run the formatter and commit the result." M fees.txt
fees.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Formatting changed files: run the formatter and commit the result.Explanation: the last line had no new line at the end, and the formatter added one. git status --porcelain lists the changed file in a fixed format, and git diff --exit-code failed (exit code 1), which a pipeline treats as a failed step, so the echo ran. In a real pipeline, that failure turns the status check red, and the pull request can't be merged until the formatted file is committed.
Common Mistakes
Relying on local hooks instead of CI. Hooks can be skipped. CI runs on every push, and protected branches can require it.
Needing history in a shallow clone. git describe, changelogs and git blame need it. Fetch the full history (fetch-depth: 0) for those jobs only.
Using the branch name from git branch in CI. HEAD is detached. Use the platform's variable.
Parsing git status or git log default output in scripts. Use --porcelain, --format and plumbing commands.
Secrets in pipeline files. Store passwords and tokens as the platform's encrypted secrets or variables, never in the committed YAML.
Under the Hood
A runner starts from nothing, so every pipeline begins with a clone or fetch of exactly one commit, often with --depth, --filter or a single branch to save time. Platforms pass the commit ID and branch to the job as environment variables, and check out that commit, which is why HEAD is detached. When the job finishes, the platform attaches the result to the commit's ID through its status API. That is how a pull request can show the checks for its newest commit, and why pushing a new commit restarts them: the old commit's results don't apply to the new one.
Interview Corner
What is CI/CD?
Continuous integration builds and tests every pushed change automatically. Continuous delivery prepares every passing change for release, and continuous deployment releases it automatically. Pushes, pull requests and tags trigger pipelines defined in files committed to the repository.
How does a pull request get a green or red check?
The platform runs the pipeline for the pull request's newest commit and reports the result as a status check on that commit. Branch protection can require passing checks before merging.
Why does git describe fail in some CI jobs?
CI often clones shallowly, without older commits or tags, so there is no tag to describe from. Fetch the full history and tags (git fetch --unshallow --tags, or fetch-depth: 0 in GitHub Actions).
How do you check in a script whether the working tree is clean?
git status --porcelain prints nothing when clean, and git diff --exit-code (plus --cached for staged changes) exits with a non-zero code when there are changes.
Why is HEAD detached in CI?
The runner checks out the exact commit that triggered the pipeline rather than a branch. Use the platform's variables for the branch name.
Practice
Challenge: name the build. In ci-checkout, which now has the full history and tags, print the version name a pipeline would use, with git describe --tags --always --dirty. Then change fees.txt without committing and print it again.
What does Git print? Before you open the solution, decide what --dirty adds, and why --always doesn't matter here.
Show solution
cd ../ci-checkout
git describe --tags --always --dirty
echo 'Exam fee: $800' >> fees.txt
git describe --tags --always --dirtyv1.0.0-1-g3550432
v1.0.0-1-g3550432-dirtyThe first name is one commit after v1.0.0. With an uncommitted change, -dirty is added, warning that this build can't be reproduced from any commit. --always would print just the short commit ID if no tag existed at all, so the command never fails.
Checkpoint. Add a pipeline file for your platform to a practice Java project, push a branch, and watch the check appear on the pull request. Then make a test fail, push again, and see the check turn red.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git fetch --unshallow --tags | Complete a shallow CI clone |
git status --porcelain | Check for changes in a script |
git diff --exit-code | Fail when files changed |
git rev-parse HEAD | Get the commit being built |
git describe --tags --always --dirty | Name the build |
git for-each-ref --format=... | List refs in a script |
Was this tutorial helpful?