Skip to main content
Git & GitHub Essentials for Security Engineers
Lesson 3 of 10

Git & GitHub Essentials for Security Engineers

Manage security scripts safely with Git branches and Pull Requests, inspect commit history for accidental secrets, and understand why exposed .git directories leak source code.

Beginner 20 min Programming

Learning objectives

  • Explain how Git tracks changes across the working tree, staging area, and commit history
  • Use git status, git diff, git log, and git show to audit changes and recover removed lines
  • Identify why deleting a secret in a new commit does not remove it from Git history
  • Prevent source and credential leaks via .gitignore, secret rotation, and blocking web access to .git

Recommended first: Python Fundamentals for Security TasksRegular Expressions for Logs & Indicator Extraction

#Why this matters

As you write Python triage scripts and regex log parsers, you need a safe way to track changes, test edits on separate branches, and share code with teammates. Git handles local version history, while GitHub hosts remote repositories for collaboration and Pull Request (PR) code review.

For security engineers, Git is both a daily tool and a common source of leaks. Because Git keeps every committed snapshot, a password or API token removed in a second commit still lives inside the first commit. Worse, if a deployment copies the hidden .git folder onto a public web server, anyone can download that history.

#Core Git workflow and three areas

Git separates your work into three distinct places:

  1. Working tree: the files you currently see and edit in your project folder.
  2. Staging area (index): the exact changes you mark with git add for the next snapshot.
  3. Repository history (.git/): permanent commits stored locally, which you push to a remote like GitHub.
  1. Working treeedit scripts or configs
  2. Staging areaselect changes with git add
  3. Local commitrecord snapshot with git commit
  4. GitHub PRpush branch and request review

Working on a feature branch instead of editing main directly keeps half-finished scripts isolated and lets another engineer review your git diff before merging.

Command What it does
git status Shows current branch, modified files, and staged changes
git switch -c fix-parser Creates and switches to a new branch named fix-parser
git add parser.py Stages parser.py for the next commit
git diff Compares unstaged working tree edits against the index
git commit -m "Add IP check" Records staged changes as a new commit
git log --oneline Lists commits concisely with short IDs and messages
git show <hash> Displays the metadata and patch for a single commit

#Auditing history and finding buried secrets

Suppose a developer accidentally commits a test credential inside config.py, notices the mistake, deletes the line, and creates a new commit titled "Remove test key". On main, the current file looks clean. However, Git never erases the earlier snapshot when you add a new commit on top.

Using git log and git show (or git log -S to search history for commits that added or removed a specific string), an auditor can read every past line:

Auditing commit history in a lab repository
$ git log --oneline
b8f12c4 Remove test key
a319e02 Add initial config loader
$ git show a319e02
commit a319e02...
Author: Lab Dev <dev@example.test>

    Add initial config loader

diff --git a/config.py b/config.py
new file mode 100644
--- /dev/null
+++ b/config.py
@@ -0,0 +1,2 @@
+LAB_API_URL = "http://lab.local/api"
+LAB_TOKEN = "demo_token_8472"

When auditing a large repository, git log -S "LAB_TOKEN" -p jumps straight to the commits where "LAB_TOKEN" appeared or disappeared, saving you from reading hundreds of unrelated diffs.

#Exposed .git directories on web servers

Inside every repository sits a hidden .git/ directory containing HEAD (pointing to the active branch), config (remote URLs), index (tracked file list), and objects/ (compressed file blobs, trees, and commits).

In veb-tahlil labs and real-world misconfigurations, teams sometimes deploy a website by copying the entire project folder into a web server's document root without excluding .git/.

How an exposed .git directory leaks source codeHow an exposed .git directory leaks source codeClientWeb ServerGET /.git/HEAD200 OK (ref: refs/heads/main)GET /.git/index and /.git/objects/...200 OK (compressed blobs and commits)Client reconstructs source files and past secrets locally

Even when directory listing is disabled, fetching known files such as /.git/HEAD confirms whether .git is accidentally exposed over HTTP.

#Defending and common mistakes

  • Treating git rm as secret erasure: Removing a secret in a new commit leaves it visible in history. If a real key or password was ever pushed, rotate (revoke) the credential first, because anyone who fetched the repository may already hold a copy.
  • Skipping .gitignore: Create a .gitignore file before your first git add so .env files, private keys (*.pem, *.key), and temporary dumps are never staged by accident.
  • Using git add . blindly: Review git status and git diff before staging so stray artifacts do not slip into commits.
  • Serving .git/ in production: Configure web servers and reverse proxies to deny all requests to /.git, or build clean deployment packages that omit .git/ entirely.
  • Merging without review: Enable branch protection on main in GitHub so every change requires a Pull Request review and automated secret scanning checks.

#Summary

Git tracks code across a working tree, staging area, and immutable commit history, while GitHub adds branches and Pull Requests for peer review. Because commands like git log -p, git show, and git log -S reveal every historical snapshot, deleting a committed secret in a later commit does not hide it. Protect repositories by maintaining .gitignore, rotating leaked credentials immediately, reviewing diffs before pushing, and blocking HTTP access to .git/ on web servers.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge