squash2stash
v0.0.1
Published
> Script that squashes your current git branch changes and stashes it.
Downloads
77
Readme
squash2stash
Script that squashes your current git branch changes and stashes it.
Motivation
Like many developers, I found myself creating lots of commits during development with messages like "WIP" or "fixed XYZ". While these commits helped me track my progress, I realized they:
- Made my commit history messy
- Sometimes turned rebasing into a time-consuming mess
My solution to these problems:
Squash all changes from your working branch into a single change and stash it. You can then:
- Create a new "clean" branch
- Apply the stashed changes
- Create a single, well-documented commit
- Rebase if needed
Installation
Put this into your .zshrc
:
squash2stash() {
local target_branch=${1:-origin/main}
git fetch
local main_commit="$(git merge-base HEAD $target_branch)"
local current_branch="$(git rev-parse --abbrev-ref HEAD)"
git switch -c temp/squash2stash # create temporary branch to not change current branch directly
git reset --soft "$main_commit"
git stash
git switch $current_branch
git branch -d temp/squash2stash
}
How it works