---
name: git-workflow
description: 'Everyday git workflow for coding agents (git 2.4x): create a branch with git switch, stage selectively, write Conventional Commits, fix up earlier commits and autosquash non-interactively, rebase onto main, push with --force-with-lease, tag releases. Use when the user asks to commit, branch, rebase, clean up history or prepare a pull request. Triggers: git commit، git rebase، conventional commits، squash commits، branch workflow.'
---

# Day-to-day git workflow

## When to use

Committing work, creating branches, syncing with `main`, tidying history before a pull request. For mistakes see git-recovery; for pull requests see github-cli.

## Principles

- Look before you act: `git status` and `git diff` first.
- One logical change per commit; the build passes at every commit.
- Never rewrite history that others have pulled. Force-push only your own feature branch, and only with `--force-with-lease`.
- Never commit secrets, `.env` files or build output. Add them to `.gitignore` first.
- Do not commit or push unless the user asked for it.

## Workflow

1. Update: `git fetch --prune` then `git switch main && git pull --ff-only`.
2. Branch: `git switch -c feat/export-epub`.
3. Work in small steps; stage precisely, commit with a Conventional Commit message.
4. Before the pull request: rebase on the updated `main`, run tests, push.

```bash
git status -sb
git switch -c fix/rtl-tables            # new branch from the current one
git switch -                            # back to the previous branch

git add -p                              # choose hunks (needs a terminal)
git add src/render.ts tests/render.test.ts   # or list files explicitly
git restore --staged src/render.ts      # unstage
git restore src/render.ts               # discard unstaged edits (destructive)

git diff --staged --stat
git commit -m "fix(render): keep table columns right-aligned in RTL"
git commit --amend --no-edit            # fold staged changes into the last commit (unpushed only)
```

## Conventional commits

Format: `type(scope): imperative summary` (about 72 characters), optional body explaining why, footer `BREAKING CHANGE: ...` or `Closes #123`.

Types: `feat` `fix` `docs` `style` `refactor` `perf` `test` `build` `ci` `chore` `revert`. A breaking change adds `!`: `feat(api)!: drop the v1 envelope`.

```bash
git commit -m "feat(export): add EPUB format" -m "Uses the shared theme so RTL books keep their cover.

Closes #42"
```

## Tidy history without an editor

```bash
git commit --fixup=<sha>                                  # mark a correction for an earlier commit
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash origin/main   # applies fixups, no editor opens
git rebase origin/main                                    # replay your commits on the latest main
git rebase --continue                                     # after resolving a conflict (git add first)
git rebase --abort                                        # give up and return to the start
git push --force-with-lease                               # after a rebase of your own branch
```

## Inspect

```bash
git log --oneline --graph --decorate -20
git log -p -S"functionName" -- src/       # when did this string appear or vanish
git blame -L 10,30 src/render.ts
git diff main...HEAD --stat               # what this branch adds
git bisect start HEAD v1.2.0              # then: git bisect good | bad | reset
```

## Stash and worktrees

```bash
git stash push -m "wip: layout" -- src/   # park changes
git stash list && git stash pop
git worktree add ../repo-hotfix hotfix/login   # second checkout, no stashing needed
```

## Tags and releases

```bash
git tag -a v1.4.0 -m "v1.4.0"
git push origin v1.4.0
```

## Useful config

```bash
git config --global pull.rebase true
git config --global init.defaultBranch main
git config --global rerere.enabled true    # reuse recorded conflict resolutions
```

## Gotchas

- `git add -A` stages unrelated and untracked files; name the paths.
- `git commit -a` skips new files.
- Interactive commands (`git add -i`, `git rebase -i` without `GIT_SEQUENCE_EDITOR`) hang in non-interactive shells.
- A rebase conflict is not an error: edit the files, `git add`, `git rebase --continue`.

## Verify

```bash
git status -sb                   # clean, and ahead/behind as expected
git log --oneline origin/main..HEAD
git diff origin/main --stat
```
