Skip to content

Commit 45eddfd

Browse files
authored
chore: add squash_since_main
1 parent c2aaba0 commit 45eddfd

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

.functions

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,51 @@ standup() {
267267
}
268268
}'
269269
}
270+
271+
272+
squash_since_main() {
273+
echo "Preparing to squash all commits since origin/main..."
274+
275+
# Fetch latest from origin
276+
git fetch origin main --quiet
277+
278+
# Define commit range
279+
COMMIT_RANGE="origin/main..HEAD"
280+
281+
# Count commits in the range
282+
COMMIT_COUNT=$(git rev-list --count $COMMIT_RANGE)
283+
284+
if [[ $COMMIT_COUNT -eq 0 ]]; then
285+
echo "No commits found between origin/main and HEAD."
286+
return
287+
fi
288+
289+
echo "Found $COMMIT_COUNT commits to squash."
290+
291+
# Get the current branch name
292+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
293+
294+
# Create a temporary file for the commit message
295+
TEMP_MSG_FILE=$(mktemp)
296+
297+
# Start with a title line
298+
echo "Squashed $COMMIT_COUNT commits from branch $CURRENT_BRANCH" > "$TEMP_MSG_FILE"
299+
echo "" >> "$TEMP_MSG_FILE"
300+
echo "Original commits:" >> "$TEMP_MSG_FILE"
301+
302+
# Add bullet points for each commit
303+
git log $COMMIT_RANGE --pretty=format:"* %s (%h)" --reverse >> "$TEMP_MSG_FILE"
304+
# Get the base commit for rebasing
305+
BASE_COMMIT=$(git merge-base HEAD origin/main)
306+
307+
# Perform the squash using reset and commit
308+
echo "Squashing commits..."
309+
git reset --soft $BASE_COMMIT
310+
git commit --file="$TEMP_MSG_FILE" -S
311+
312+
# Clean up
313+
rm "$TEMP_MSG_FILE"
314+
315+
echo "Squash complete. Pushing with lease..."
316+
git push --force-with-lease
317+
}

0 commit comments

Comments
 (0)