Browse Learn Clojure Foundations as a Java Developer

Use Git Commands in Clojure Projects

Apply everyday Git commands to Clojure work by staging source changes deliberately, reviewing namespace diffs, keeping generated artifacts out, and syncing branches safely.

Most Git commands are the same in Clojure as in Java. The difference is what you pay attention to in the diff. Clojure changes often alter data shapes, namespace dependencies, tests, and small pure functions rather than large class files. Your Git workflow should make those changes easy to review.

The Daily Loop

Use this loop when editing Clojure code:

1git status --short
2git diff
3git add src/order_service/pricing.clj test/order_service/pricing_test.clj
4git diff --cached
5git commit -m "Clarify pricing calculation"

The critical step is git diff --cached. It lets you verify that the commit contains source changes, test changes, and no generated artifacts before you create history.

Commands You Will Use Constantly

Command Clojure-specific habit
git status --short Check for generated files such as target/, .cpcache/, logs, or REPL artifacts.
git diff Read namespace imports, data shape changes, and test expectations carefully.
git add <path> Stage related source and test files together instead of blindly adding everything.
git restore --staged <path> Unstage accidental artifacts without deleting the working copy.
git commit -m "..." Write messages around behavior, not line-count movement.
git log --oneline -- <path> Understand why a namespace or test changed before rewriting it.

Avoid using git add . as a reflex in Clojure projects with active REPLs or build tools. It is easy to stage local artifacts when a project has generated files.

Branch and Sync Commands

Modern Git supports git switch; older workflows may still use git checkout.

1git switch -c feature/pricing-rules
2git fetch origin
3git rebase origin/main
4git push -u origin feature/pricing-rules

For a shared branch policy, follow the team’s rule. The important Clojure habit is to sync before review so reviewers are not sorting through stale namespace conflicts and outdated test failures.

Task Safer command pattern
Create a branch git switch -c feature/name
Update branch from remote git fetch origin then rebase or merge according to team policy
See recent work git log --oneline --decorate --graph -20
See one file’s history git log --follow -- path/to/file.clj
Compare with main git diff origin/main...HEAD

Review Clojure Diffs Differently

Java diffs often focus on class methods, visibility, and object boundaries. Clojure diffs often need a different review lens.

Diff area What to check
ns form New dependencies, aliases, circular require risk, and unused imports.
Function signature Whether callers and tests still match the data shape.
Data literals Keyword names, nested map shape, defaults, and missing-case behavior.
Tests Whether expectations describe behavior rather than implementation trivia.
Build files Whether dependency or classpath changes affect REPL, test, and production commands.

This is why small commits matter. A commit that changes data shape, namespace wiring, and packaging all at once is hard to review even if the line count is small.

Undo Without Destroying Work

Use non-destructive commands first.

Goal Command
Unstage a file git restore --staged path/to/file.clj
See what changed in one file git diff -- path/to/file.clj
Discard local edits in one file only git restore path/to/file.clj
Keep work but move it off the branch temporarily git stash push -m "message"
Inspect a previous version git show HEAD~1:path/to/file.clj

Be careful with destructive commands such as hard resets. They can erase valuable REPL-to-source work if you have not committed or stashed it.

A Commit Checklist

Before committing Clojure work:

  1. Run the relevant test command.
  2. Run the formatter if the project has one.
  3. Inspect git diff --cached.
  4. Confirm build outputs and REPL artifacts are unstaged.
  5. Make the commit message describe the behavior or boundary changed.

Knowledge Check

### Why is `git diff --cached` especially useful before a Clojure commit? - [x] It shows exactly what staged source, test, and config changes will enter history - [ ] It starts a REPL automatically - [ ] It compiles Clojure namespaces - [ ] It pushes changes to a remote branch > **Explanation:** REPL and build artifacts can appear in the working tree. The staged diff prevents accidental history pollution. ### What should you check when a Clojure diff changes an `ns` form? - [x] New dependencies, aliases, and possible circular require risk - [ ] Only the number of spaces before each symbol - [ ] Whether the file has a Java package declaration - [ ] Whether every function was AOT-compiled > **Explanation:** The `ns` form controls namespace loading and dependency direction, so it is a key review point. ### Which command unstages a file without deleting the working-copy edits? - [x] `git restore --staged path/to/file.clj` - [ ] `git reset --hard` - [ ] `git clean -fd` - [ ] `git push --force` > **Explanation:** `git restore --staged` removes the file from the next commit while preserving the local edits.
Revised on Saturday, May 23, 2026