Lesson 2 of 7 · Git for DBAs

How Git Works

The working directory, staging area, and repository, commits and history, branches and merges, and ignoring files.

Beginner 5 sections 4 min read

The three areas

Git keeps your work in three places: the working directory, where you edit files; the staging area, where you collect the changes you want to save together; and the repository, where saved snapshots, called commits, are stored for good. Moving changes between them is most of what you do with Git.

WORKING DIRECTORYSTAGING AREAREPOSITORY the files you editchanges ready forthe next commitcommits: the savedhistory (.git folder) roles.sql (modified)notes.md (new) roles.sql 3f2a1c9a81d4e0… git add git commit git restore brings a file back to its last committed version
Working directory (working tree)
The normal folder of files you open and edit. Git notices when they differ from the last commit.
Staging area (index)
A holding area for the changes you want in your next commit. It lets you commit some changes now and leave others for later, so each commit contains one logical change, such as "add read-only role", even if you edited several things.
Repository (.git folder)
A hidden .git folder inside the project that stores every commit, branch, and setting. Deleting it removes the history; the files themselves stay.

So a file is always in one of three states: modified (changed but not staged), staged (marked for the next commit), or committed (safely stored in the repository).

Commits

A commit is a snapshot of all the tracked files at one moment. Each commit records:

  • A unique ID, a long code such as 3f2a1c9e…, usually shortened to its first seven characters.
  • The author, their email, and the date.
  • A message explaining the change.
  • A link to its parent, the commit before it, which is how Git builds the history.

Here's Kofi starting a repository for the team's scripts and committing the first file:

# turn the folder into a Git repository
$ cd dba-scripts
$ git init

# see what Git notices
$ git status
On branch main
Untracked files:
        sql/create_sales_ro_role.sql

# stage the file, then commit it with a message
$ git add sql/create_sales_ro_role.sql
$ git commit -m "Add read-only role for sales reporting"
[main 3f2a1c9] Add read-only role for sales reporting
 1 file changed, 6 insertions(+)

# see the history, one line per commit
$ git log --oneline
3f2a1c9 Add read-only role for sales reporting

When Kofi edits the file later, git diff shows exactly which lines changed, before staging and committing again:

$ git diff
-GRANT SELECT ON sales.orders TO sales_ro;
+GRANT SELECT ON sales.orders    TO sales_ro;
+GRANT SELECT ON sales.customers TO sales_ro;

Branches

A branch is a separate line of work. You create one to make a change without affecting anyone else, then merge it back when it's ready. The main line of history is usually called main. Older repositories often use master, which is still Git's own default unless you change it, as shown in Using Git.

MAIN create tablesadd rolesfix grantmergeadd view add-order-index index scriptrollback script HEAD
# create a branch and switch to it
$ git switch -c add-order-index

# ...edit, add, and commit as usual...

# go back to main and merge the finished branch
$ git switch main
$ git merge add-order-index

Branches are cheap and fast in Git, so teams create one for every change, however small. HEAD is Git's name for "where you are now": the branch and commit your working directory is based on.

Ignoring files

Some files should never be committed, such as passwords, log files, and large data exports. A file called .gitignore in the repository lists patterns for Git to ignore:

# .gitignore
*.log
*.dmp
*.env
wallets/
tnsnames.ora.local

Key takeaways

  • Git has three areas: the working directory, the staging area, and the repository in the .git folder.
  • git add stages changes, git commit saves them as a snapshot, and git restore discards uncommitted edits.
  • A commit has a unique ID, an author, a date, a message, and a parent, which together form the history.
  • git status, git diff, and git log show what's changed and what's happened.
  • Branches let you work on a change in isolation, then merge it back. HEAD marks where you are.
  • .gitignore keeps logs, dumps, and secrets out of the repository.