git-workflow
verifiedd2895685-0ecd-4780-ada9-c127243233a2
Safe Git branch workflow — feature branches, rebase vs merge, interactive rebase, destructive-command guardrails.
Metadata
Skill file
# Safe Git Branch Workflow
Use when managing feature branches and keeping history clean without losing work.
## Branch + commit flow
```bash
git switch -c feat/thing
git add -A
git commit -m "feat: add thing"
```
## Rebasing onto latest main (preferred over merge)
```bash
git fetch origin
git rebase origin/main
# resolve conflicts, then:
git push --force-with-lease
```
`--force-with-lease` refuses to clobber remote commits that changed since your
last fetch — always prefer it over `--force`.
## Interactive rebase to clean up
```bash
git rebase -i HEAD~5 # squash/fixup/reword
```
## Recovering from mistakes
- Accidental reset: `git reflog` then `git reset --hard <sha>`.
- Uncommitted work lost: `git stash` is the checkpoint; `git fsck --lost-found` as last resort.
## Guardrails
- Never `git push --force` on a shared branch others may have pulled.
- Never `git reset --hard` without a stash or reflog stake in the outcome.
- Commit messages: imperative mood, `type: subject` convention; avoid `&` and
unbalanced quotes when a tool parses your output.
## Pitfalls
- Rebasing a branch that is already merged creates duplicate commits — check with the remote first.
- Long-lived branches drift: rebase frequently, not just before merge.