DevOps Tutorial · Git and CI/CD · Lesson 11 of 25

Git Basics

Track changes with Git: repositories, commits, the staging area, history and undoing mistakes.

Create a free account to save this page, take notes and track your progress. Sign in

Why Git matters in DevOps

Everything starts with a commit: application code, pipeline definitions, Dockerfiles, Terraform and Kubernetes manifests all live in Git, so every change is reviewed, versioned and can trigger automation.

The core workflow

git clone https://github.com/acme/api.git
git status                  # what changed?
git add src/app.py          # stage a change
git commit -m "Add health endpoint"
git push                    # share it; CI starts
Commit and inspect history
git init -q -b main api && cd api
echo 'print("hello")' > app.py
git add app.py
git commit -qm "Add app"
echo 'print("health: ok")' >> app.py
git status --short
git commit -qam "Add health output"
git log --oneline

Output

 M app.py
cb43f0c Add health output
1a5901f Add app

Run this on your computer — see “Get Started” for setup.

Looking at history

git log --oneline, git show <commit>, git diff (unstaged), git diff --staged and git blame file answer "what changed, when and why".

Undoing things

Situation Command
Discard local edits to a file git restore file
Unstage a file git restore --staged file
Undo a pushed commit safely git revert <commit> (a new commit that reverses it)
Rewrite unpushed local commits git commit --amend, git rebase -i

Never rewrite history that others have pulled (no force-push to main). git revert is the safe way to undo a bad change in production code.

Undo a bad commit with revert
git init -q -b main api && cd api
echo "timeout=30" > config.ini && git add . && git commit -qm "Add config"
echo "timeout=1" > config.ini && git commit -qam "Lower timeout"        # a bad change
git revert --no-edit HEAD > /dev/null
git log --oneline
cat config.ini

Output

c29b8f4 Revert "Lower timeout"
ed26f90 Lower timeout
323418b Add config
timeout=30

Run this on your computer — see “Get Started” for setup.

Test yourself

A bad commit is already on the shared main branch. What's the safe way to undo it?

  1. git reset --hard and force-push
  2. git revert <commit>
  3. Delete the repository
  4. git commit --amend
Show answer

Answer: git revert <commit>

revert adds a new commit that reverses the change, without rewriting history others already have.

Was this page helpful?