Last active
July 7, 2025 13:17
-
-
Save MartinWallgren/5c00716d176350a842f46037a79f9230 to your computer and use it in GitHub Desktop.
Select a fixup commit using fzf
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
#!/bin/sh | |
# SPDX-FileCopyrightText: 2019 Martin Wallgren | |
# | |
# SPDX-License-Identifier: MIT | |
function usage() { | |
echo "Git command to help you select which commit to create a fixup commit for." | |
echo "" | |
echo "The command will let you select a commit from a range and commit the current" | |
echo "staging area using the selected commit as argument to the --fixup= option. Any" | |
echo "extra options passed to this command will be forwarded to the git commit" | |
echo "command." | |
echo "" | |
echo "The range will be the current upstream to HEAD. If no upstream is set for the" | |
echo "current branch, the default range will be used. You can set the default range" | |
echo "with GIT_DEFAULT_FIXUP_RANGE, if not set, origin/master..HEAD will be used as" | |
echo "default range." | |
echo "Example: GIT_DEFAULT_FIXUP_RANGE=origin/develop..HEAD git-fixup." | |
echo "Note that a configured upstream branch will take precedence over the default range." | |
echo "" | |
echo "This command depends on fzf (https://github.com/junegunn/fzf)" | |
echo "" | |
echo "git-fixup" | |
echo -e "\t-h --help" | |
echo "" | |
} | |
while [ ! $# -eq 0 ]; do | |
case "$1" in | |
--help | -h) | |
usage | |
exit | |
;; | |
esac | |
shift | |
done | |
upstream=$(git rev-parse --abbrev-ref @{upstream} &>/dev/null) | |
if [ -z "${upstream}" ]; then | |
fixup_range="${GIT_DEFAULT_FIXUP_RANGE:-origin/master..HEAD}" | |
else | |
fixup_range="${upstream}..HEAD" | |
fi | |
parent=$(git log --no-merges --oneline ${fixup_range} | | |
fzf --height ${FZF_TMUX_HEIGHT:-40%} | | |
cut -d' ' -f1) | |
if [ -z "${parent}" ]; then | |
echo "No commit found to fix." | |
else | |
git commit --fixup=${parent} "$@" | |
fi |
Done.
Cool that this is being used.
Thank you! Yes, I use it quite a lot. Here's my "fancied up" version with previews and a few extra goodies. I'll distribute it with my dotfiles soon (I still need to do some cleanup on it given that it used to have some unencrypted credentials that need to be filtered out of the repo), feel free to adopt anything you like.
https://gist.github.com/aaccioly/4e724ec10d892fb0f90c93cf5cebd262
Nice, I've been meaning to lookup the default branch for a long time. It's about time :)
I like the preview window.
I incorporated your changes to my dotfiles.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Martin, how are you? I'm currently using your script above with a few modifications and it's working brilliantly.
I would like to redistribute it with my dotfiles. Would you be willing to add a license to it?
E.g., the following should suffice: