Lesson 4 of 12 - Foundations: the server, the network and Git

Git for DevOps: commits, branches and what stays out

By Branko Petric - Updated - About 7 min - Free

The short answer

Git records changes to files as commits. You do work on a branch, open a pull request so someone reviews it, then merge it into main. Some things never go in Git: passwords, API keys, .env files and build output. A .gitignore keeps them out before they are committed.

Already know this? Skip to the 3 questions

What Git does

Git is a tool that records the history of a folder of files. Each saved change is a commit: a snapshot of what changed, who changed it, when, and a message saying why.

For DevOps work, Git is where everything lives. Application code, Dockerfiles, Terraform, pipeline configs and runbooks all go in Git. If it is not in Git, nobody can review it, and nobody can rebuild it after it is gone.

Commits

The basic loop is: change files, stage them, commit.

git status                      # what changed
git diff                        # the exact lines that changed
git add nginx/site.conf         # stage this file for the next commit
git commit -m "Raise nginx client_max_body_size to 20m for uploads"
git log --oneline -5            # last five commits

Write the message for the person reading it in six months. "fix" says nothing. "Raise nginx upload limit to 20m for uploads" tells them what and why. Keep each commit to one logical change, so it is easy to review and easy to undo with git revert.

Branches and pull requests

main is the branch that usually gets deployed. You do not work on it directly. You create a branch, commit there, and push it.

git switch -c raise-upload-limit
# edit, add, commit
git push -u origin raise-upload-limit

Then you open a pull request (PR) on GitHub or GitLab. A PR shows the diff, lets a teammate leave comments, and usually triggers the CI pipeline to run tests. When the review passes and the checks are green, the PR is merged into main.

This matters because main is shared. If you rewrite its history with git push --force, you can wipe out commits your teammates already pushed. Most teams protect main so that nobody can force-push to it and every change goes through a PR.

What never goes in Git

Git keeps every version of every file forever. Deleting a file in a new commit does not remove it from history. So some things must never be committed:

  • Secrets. Passwords, API keys, tokens, private keys, .env files with real values. If one is committed, treat it as leaked: revoke it and create a new one.
  • Generated files. node_modules/, build output like dist/, compiled binaries. They can be rebuilt from source.
  • Local and machine files. Editor settings, .DS_Store, log files.
  • Large binary data. Database dumps and videos make every clone slow.

A .gitignore file in the repo root tells Git which files to skip:

# .gitignore
.env
.env.*
!.env.example
node_modules/
dist/
*.log
*.pem
.DS_Store

The ! line makes an exception, so .env.example (with placeholder values) can still be committed. Commit the .gitignore itself so the whole team gets the same rules.

Note that .gitignore only affects files Git is not already tracking. If .env was committed before you added the rule, you also need git rm --cached .env, and you still need to rotate the secrets inside it.

Common beginner mistakes

  • Running git add . without looking. It stages everything, including that .env you forgot about. Run git status and read the list first.
  • Thinking a delete commit removes a secret. The secret is still in history. Revoke it and issue a new one.
  • Force-pushing to a shared branch. It can erase teammates' work. Use git revert to undo a change on main.

Check your understanding

3 questions. Retry as often as you like - nothing counts against you.

Question 1 of 3

Which of these should NOT be committed to a Git repository?

Select all that apply - one or more answers can be right.

Which of these should NOT be committed to a Git repository?

Want the bigger picture? This lesson maps to this step of the DevOps roadmap.