Skip to content
Pwnversity
FUND-GITbasic · 12 min · upd. 2026-11

Git for Hacking

Git is the version control behind almost all the software you’ll audit and almost every tool you’ll clone. Understanding how it stores history pays off twice: you handle your own arsenal comfortably, and you know where to look for what others thought they had deleted. A repository doesn’t forget: a password that was once in a commit is still there even if the file no longer exists.

Area What it is Command that moves things there
Working directory The files you see and edit Editing, git restore
Staging (index) What will go into the next commit git add
Repository (.git/) Every commit, branch and object git commit

The normal flow is edit → git add (stage) → git commit (save the snapshot). git status tells you at any moment what’s in each area.

Inside .git/objects Git stores three kinds of object, each identified by the hash of its content:

Object Stores Example
blob A file’s content, without its name The text of config.php
tree A directory: names pointing to blobs and other trees src/, app.py
commit A root tree + author, date, message and the parent commit “Add login”

A commit doesn’t store differences: it stores a complete snapshot (its tree) and a pointer to its parent. Chaining parents gives you the history. If two commits contain the same unchanged file, both point to the same blob, which is why it doesn’t take double the space.

A branch is a text file holding a commit hash (.git/refs/heads/main). HEAD says which branch you’re on (ref: refs/heads/main). Committing moves the current branch to the new commit; creating a branch just creates another pointer.

gitGraph
    commit id: "A: first version"
    commit id: "B: add config"
    branch dev
    checkout dev
    commit id: "D: test on dev"
    checkout main
    commit id: "C: remove config"
    merge dev id: "E: merge dev"

In the diagram, main points to E and dev to D. Commits A–E all exist in .git/objects, whether or not a branch points at them.

Command What it does Rewrites history?
git restore file Discards unstaged changes to the file No
git restore --staged file Unstages it, leaving the content alone No
git revert <hash> Creates a new commit that undoes another No: it adds
git reset --soft HEAD~1 Moves the branch back; keeps the changes staged Yes (locally)
git reset --hard HEAD~1 Moves the branch back and discards the changes Yes (locally)

reset moves the branch pointer; the commits left “behind” aren’t deleted at that point. The reflog (.git/logs/) records every HEAD movement for weeks, so a “lost” commit is recovered with git reflog and git reset --hard <hash>. Orphaned objects only disappear when the garbage collector (git gc) prunes them, and by default it respects a grace period.

.gitignore only stops Git from starting to track a file. If .env was committed once and then added to .gitignore, it’s still in every earlier commit. Removing it with git rm --cached keeps it out of the next commit, not the past ones. To really remove it you have to rewrite history (git filter-repo) and rotate the secret, because any earlier clone already has it.

Path Content
HEAD The current branch
config Remotes (url = …), sometimes with a username and token embedded
refs/heads/, refs/tags/ Hashes of each branch and tag
objects/ Every blob, tree and commit (loose or in pack/)
logs/ The reflog: movements of HEAD and of each branch
index The staging area, with the list of tracked files

Those files are enough to rebuild the whole project, history included. That’s why a .git/ reachable from a website amounts to publishing the source code.

Every commit carries user.name and user.email exactly as whoever made it configured them (git config --global user.name …). Git doesn’t verify them: anyone can commit as someone else unless the project requires signed commits (GPG or SSH). For you, they’re a source of internal names and email addresses.

A lab repo in /tmp where someone committed a secret and then “removed” it:

Ventana de terminal
$ git init -b main demo && cd demo
$ echo "print('hello')" > app.py
$ git add app.py && git commit -m "First version"
$ echo "DB_PASS=Sup3rS3cret" > .env
$ git add .env && git commit -m "Config"
$ git rm --cached .env && echo ".env" > .gitignore
$ git add .gitignore && git commit -m "Remove .env"
$ git log --oneline --graph --all
* 8ccf323 (HEAD -> main) Remove .env
* 9960b48 Config # this is where the secret went in
* 7e9ddd7 First version

Search the whole history, not just the current state:

Ventana de terminal
$ git log -S 'DB_PASS' --oneline --all # commits that add or remove that string
8ccf323 Remove .env
9960b48 Config
$ git log -p --all -- .env # the file's content in every commit
...
-DB_PASS=Sup3rS3cret # the value is still in history
$ git show 9960b48:.env # read the file as it was in that commit
DB_PASS=Sup3rS3cret

Recover a commit someone “deleted” with reset:

Ventana de terminal
$ git reset --hard HEAD~1 # main goes back to 9960b48
$ git reflog | head -3
9960b48 HEAD@{0}: reset: moving to HEAD~1
8ccf323 HEAD@{1}: commit: Remove .env # still findable
$ git reset --hard 8ccf323 # and it's back

Look inside a commit:

Ventana de terminal
$ git cat-file -t HEAD # object type
commit
$ git cat-file -p HEAD # content
tree 4d8d7b0013b3... # the complete snapshot
parent 9960b48... # the previous commit (Config)
author ana <ana@lab.local> 1793915784 +0100

Check every branch and tag, which is where tests and pre-production versions tend to linger:

Ventana de terminal
$ git branch -a # local and remote branches
$ git tag # tagged versions
$ git log --all --format='%h %an <%ae> %s' # who did what, with email
$ git config --get remote.origin.url # is there a token inside?
  • .git/ exposed on a website: rebuild it and review the history → content discovery.
  • Secrets in the organisation’s public repos (GitHub, GitLab, gists) → code recon.
  • Secret management and why it fails in pipelines and repos → secrets in DevSecOps.
Concept What to remember
Three areas Working → add → staging → commit → repo
Commit Complete snapshot + parent, identified by its hash
Branch · HEAD Pointers to a commit · the branch you’re on
revert vs reset Adds a commit that undoes · moves the pointer back
Reflog Records HEAD movements: “deleted” things come back
.gitignore Doesn’t remove already-committed files from history
git log -S 'text' --all Commits that add or remove a string
git show <hash>:path The file as it was in that commit
.git/config Remotes, sometimes with credentials
--depth 1 No history: useless for secret hunting
Commit author Anyone can set it; handy for names and emails
  1. What does a commit store: the differences from the previous one, or a complete snapshot?

    Answer

    A complete snapshot of the project (a root tree) plus a pointer to its parent. Git computes differences on the fly when you run diff or log -p.

  2. A developer adds .env to .gitignore and runs git rm --cached .env. Is the password now unreadable?

    Answer

    No: it’s still in the earlier commits. git log -p --all -- .env or git show <hash>:.env will show it. History would have to be rewritten and, above all, the password rotated.

  3. In terms of files inside .git/, what is a branch?

    Answer

    A file under .git/refs/heads/ holding the hash of the commit it points to. HEAD is another file saying which branch is active.

  4. Someone ran git reset --hard HEAD~3 by mistake. Are those three commits gone?

    Answer

    Not right away. They’re still in .git/objects and the reflog records where HEAD was. git reflog gives you the hash and git reset --hard <hash> brings them back.

  5. When would you use revert instead of reset?

    Answer

    When the branch is already shared. revert adds a commit that undoes the change without rewriting history; reset plus push --force breaks everyone else’s copies.

  6. You cloned a repo with --depth 1 and find nothing in its history. What happened?

    Answer

    A shallow clone only brings the latest commit, so there’s no history to review. Clone in full (or extend it with git fetch --unshallow).

  7. Why is a .git/ reachable from the web so serious even if the site never links to it?

    Answer

    Because HEAD, refs and objects are enough to rebuild the full source code and its whole history, including secrets deleted later and, sometimes, credentials in .git/config.