diff --git a/Custom-Commands-Compendium.md b/Custom-Commands-Compendium.md index 9a345a2..fa288cd 100644 --- a/Custom-Commands-Compendium.md +++ b/Custom-Commands-Compendium.md @@ -447,4 +447,47 @@ customCommands: body: "Are you sure you want to commit?" command: "git commit --message '{{.Form.Type}}{{.Form.Scope}}{{.Form.Breaking}}: {{.Form.Message}}'" loadingText: "Creating conventional commit..." +``` + +## Disentangle branch + +Often you'll have a branch which doesn't do much but has a bunch of messy merge commits from merging in the main branch. If you want to convert that to just a single commit, i.e. disentangle the branch, you can use this custom command: + +```yaml +customCommands: + - key: 'K' + description: "Disentangle: Squash all changes into a single commit and rebase onto the selected branch" + context: localBranches + command: | + #!/bin/bash + + # Set the base branch + BASE_BRANCH="{{.SelectedLocalBranch.Name}}" + + # Check if the working tree is dirty + if [[ -n $(git status --porcelain) ]]; then + echo "Error: Working tree is dirty. Please commit or stash your changes before running this script." + exit 1 + fi + + # Get the merge base commit + merge_base=$(git merge-base $BASE_BRANCH HEAD) + + # Get the first commit hash, message, and author details + first_commit_hash=$(git rev-list --reverse $merge_base..HEAD | head -n 1) + first_commit_message=$(git log -1 --format=%B $first_commit_hash) + + # Reset to the merge base + git reset $merge_base + + # Stage all changes + git add -A + + # Create a new commit with all the changes, using the first commit's message and author + GIT_AUTHOR_NAME="$(git log -1 --format='%an' $first_commit_hash)" \ + GIT_AUTHOR_EMAIL="$(git log -1 --format='%ae' $first_commit_hash)" \ + git commit -m "$first_commit_message" + + # Rebase onto the base branch + git rebase $BASE_BRANCH ``` \ No newline at end of file