مهارةEnglish

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.

تنزيل SKILL.md الملف الخام (Markdown)

التثبيت في Claude Code

ضع الملف في مجلد باسم المهارة داخل مجلد skills الخاص بـ Claude Code، ثم ابدأ جلسة جديدة. الأمر التالي يفعل ذلك:

bash
mkdir -p ~/.claude/skills/git-workflow && \
  curl -fsSL https://md2.dev/library/raw/git-workflow.md \
  -o ~/.claude/skills/git-workflow/SKILL.md

المعاينة

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

العودة إلى المهارات