Git Advanced Workflows

Git Advanced Workflows

Guides an AI agent through advanced Git work: interactive rebase, cherry-picking, bisect, worktrees and reflog recovery, to keep history clean and fix repository mishaps.

Skill files2 files

references/

SKILL.md204 lines
1
---
2
name: git-advanced-workflows
3
description: Master advanced Git workflows including rebasing, cherry-picking, bisect, worktrees, and reflog to maintain clean history and recover from any situation. Use when managing complex Git histories, collaborating on feature branches, or troubleshooting repository issues.
4
---
5
6
# Git Advanced Workflows
7
8
Master advanced Git techniques to maintain clean history, collaborate effectively, and recover from any situation with confidence.
9
10
## When to Use This Skill
11
12
- Cleaning up commit history before merging
13
- Applying specific commits across branches
14
- Finding commits that introduced bugs
15
- Working on multiple features simultaneously
16
- Recovering from Git mistakes or lost commits
17
- Managing complex branch workflows
18
- Preparing clean PRs for review
19
- Synchronizing diverged branches
20
21
## Core Concepts
22
23
### 1. Interactive Rebase
24
25
Interactive rebase is the Swiss Army knife of Git history editing.
26
27
**Common Operations:**
28
29
- `pick`: Keep commit as-is
30
- `reword`: Change commit message
31
- `edit`: Amend commit content
32
- `squash`: Combine with previous commit
33
- `fixup`: Like squash but discard message
34
- `drop`: Remove commit entirely
35
36
**Basic Usage:**
37
38
```bash
39
# Rebase last 5 commits
40
git rebase -i HEAD~5
41
42
# Rebase all commits on current branch
43
git rebase -i $(git merge-base HEAD main)
44
45
# Rebase onto specific commit
46
git rebase -i abc123
47
```
48
49
### 2. Cherry-Picking
50
51
Apply specific commits from one branch to another without merging entire branches.
52
53
```bash
54
# Cherry-pick single commit
55
git cherry-pick abc123
56
57
# Cherry-pick range of commits (exclusive start)
58
git cherry-pick abc123..def456
59
60
# Cherry-pick without committing (stage changes only)
61
git cherry-pick -n abc123
62
63
# Cherry-pick and edit commit message
64
git cherry-pick -e abc123
65
```
66
67
### 3. Git Bisect
68
69
Binary search through commit history to find the commit that introduced a bug.
70
71
```bash
72
# Start bisect
73
git bisect start
74
75
# Mark current commit as bad
76
git bisect bad
77
78
# Mark known good commit
79
git bisect good v1.0.0
80
81
# Git will checkout middle commit - test it
82
# Then mark as good or bad
83
git bisect good # or: git bisect bad
84
85
# Continue until bug found
86
# When done
87
git bisect reset
88
```
89
90
**Automated Bisect:**
91
92
```bash
93
# Use script to test automatically
94
git bisect start HEAD v1.0.0
95
git bisect run ./test.sh
96
97
# test.sh should exit 0 for good, 1-127 (except 125) for bad
98
```
99
100
### 4. Worktrees
101
102
Work on multiple branches simultaneously without stashing or switching.
103
104
```bash
105
# List existing worktrees
106
git worktree list
107
108
# Add new worktree for feature branch
109
git worktree add ../project-feature feature/new-feature
110
111
# Add worktree and create new branch
112
git worktree add -b bugfix/urgent ../project-hotfix main
113
114
# Remove worktree
115
git worktree remove ../project-feature
116
117
# Prune stale worktrees
118
git worktree prune
119
```
120
121
### 5. Reflog
122
123
Your safety net - tracks all ref movements, even deleted commits.
124
125
```bash
126
# View reflog
127
git reflog
128
129
# View reflog for specific branch
130
git reflog show feature/branch
131
132
# Restore deleted commit
133
git reflog
134
# Find commit hash
135
git checkout abc123
136
git branch recovered-branch
137
138
# Restore deleted branch
139
git reflog
140
git branch deleted-branch abc123
141
```
142
143
## Detailed patterns and worked examples
144
145
Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
146
147
## Best Practices
148
149
1. **Always Use --force-with-lease**: Safer than --force, prevents overwriting others' work
150
2. **Rebase Only Local Commits**: Don't rebase commits that have been pushed and shared
151
3. **Descriptive Commit Messages**: Future you will thank present you
152
4. **Atomic Commits**: Each commit should be a single logical change
153
5. **Test Before Force Push**: Ensure history rewrite didn't break anything
154
6. **Keep Reflog Aware**: Reflog is your safety net, but entries for commits not reachable from the current tip expire after 30 days by default (`gc.reflogExpireUnreachable`), and reachable entries after 90 days (`gc.reflogExpire`)
155
7. **Branch Before Risky Operations**: Create backup branch before complex rebases
156
157
```bash
158
# Safe force push
159
git push --force-with-lease origin feature/branch
160
161
# Create backup before risky operation
162
git branch backup-branch
163
git rebase -i main
164
# If something goes wrong
165
git reset --hard backup-branch
166
```
167
168
## Common Pitfalls
169
170
- **Rebasing Public Branches**: Causes history conflicts for collaborators
171
- **Force Pushing Without Lease**: Can overwrite teammate's work
172
- **Losing Work in Rebase**: Resolve conflicts carefully, test after rebase
173
- **Forgetting Worktree Cleanup**: Orphaned worktrees consume disk space
174
- **Not Backing Up Before Experiment**: Always create safety branch
175
- **Bisect on Dirty Working Directory**: Commit or stash before bisecting
176
177
## Recovery Commands
178
179
```bash
180
# Abort operations in progress
181
git rebase --abort
182
git merge --abort
183
git cherry-pick --abort
184
git bisect reset
185
186
# Restore file to version from specific commit
187
git restore --source=abc123 path/to/file
188
189
# Undo last commit but keep changes
190
git reset --soft HEAD^
191
192
# Undo last commit and discard changes
193
# WARNING: --hard also deletes uncommitted changes (staged and unstaged),
194
# and the reflog cannot recover them. Check `git status` and stash first.
195
git reset --hard HEAD^
196
197
# Recover a deleted branch soon. By default, git gc can prune HEAD's reflog
198
# entries for unmerged commits once they're 30 days old, counted from when
199
# each entry was recorded, not from the deletion
200
# Deleting a branch also deletes its own reflog; search HEAD's reflog
201
git reflog
202
git branch recovered-branch abc123
203
```
204

© Seth Hobson · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…