Created
March 10, 2026 10:50
-
-
Save chrisweight/99a948fb132204b5a988105d51bc17e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Delete all local branches that have been deleted on the remote | |
| function gfd() { | |
| git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D | |
| } | |
| # Squash a branch to a single commit and force push it to the remote | |
| function gspf() { | |
| local current_branch=$(git branch --show-current) | |
| [[ "$current_branch" == "main" || "$current_branch" == "master" ]] && { echo "Aborting: You are on main!"; return 1 } | |
| git reset --soft $(git merge-base main HEAD) && git commit -m "$*" && git push origin HEAD -f | |
| } | |
| # Squash N commits from HEAD back to a specific commit (inclusive) on a branch to a single commit and force push | |
| function gsfc() { | |
| local target_commit="$1" | |
| local commit_msg="$2" | |
| # 1. Require a commit hash | |
| if [[ -z "$target_commit" ]]; then | |
| echo "Error: Missing commit hash." | |
| echo "Usage: gsfc <commit-hash> [\"Optional commit message\"]" | |
| return 1 | |
| fi | |
| # 2. Protect main/master branches | |
| local current_branch=$(git branch --show-current) | |
| if [[ "$current_branch" == "main" || "$current_branch" == "master" ]]; then | |
| echo "Aborting: You are on $current_branch!" | |
| return 1 | |
| fi | |
| # 3. Perform the reset | |
| # Using '&&' ensures we don't commit/push if the reset fails (e.g. bad hash) | |
| git reset --soft "${target_commit}~1" && \ | |
| # 4. Commit (uses editor if no message was provided) | |
| if [[ -n "$commit_msg" ]]; then | |
| git commit -m "$commit_msg" | |
| else | |
| git commit | |
| fi && \ | |
| # 5. Force push | |
| git push --force | |
| } | |
| # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment