Day-to-day git workflow: branches, commits, rebase and conventional commits
A safe, non-interactive git routine for agents: feature branches, small conventional commits, fixup and autosquash, rebasing onto main, and pushing with force-with-lease.
Install in Claude Code
Put the file in a folder named after the skill inside Claude Code's skills folder, then start a new session. This command does it:
bash
mkdir -p ~/.claude/skills/git-workflow && \
curl -fsSL https://md2.dev/library/raw/git-workflow.md \
-o ~/.claude/skills/git-workflow/SKILL.md Preview
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 statusandgit difffirst. - 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,
.envfiles or build output. Add them to.gitignorefirst. - Do not commit or push unless the user asked for it.
Workflow
- Update:
git fetch --prunethengit switch main && git pull --ff-only. - Branch:
git switch -c feat/export-epub. - Work in small steps; stage precisely, commit with a Conventional Commit message.
- 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 branchInspect
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 | resetStash 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 neededTags and releases
BASH
git tag -a v1.4.0 -m "v1.4.0"
git push origin v1.4.0Useful config
BASH
git config --global pull.rebase true
git config --global init.defaultBranch main
git config --global rerere.enabled true # reuse recorded conflict resolutionsGotchas
git add -Astages unrelated and untracked files; name the paths.git commit -askips new files.- Interactive commands (
git add -i,git rebase -iwithoutGIT_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