Skip to content

Instantly share code, notes, and snippets.

@aaccioly
Forked from MartinWallgren/git-fixup
Last active July 7, 2025 12:38
Show Gist options
  • Select an option

  • Save aaccioly/4e724ec10d892fb0f90c93cf5cebd262 to your computer and use it in GitHub Desktop.

Select an option

Save aaccioly/4e724ec10d892fb0f90c93cf5cebd262 to your computer and use it in GitHub Desktop.

Revisions

  1. aaccioly revised this gist Jul 7, 2025. 1 changed file with 27 additions and 8 deletions.
    35 changes: 27 additions & 8 deletions git-fixup
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,14 @@
    #!/bin/sh
    # SPDX-FileCopyrightText: 2019 Martin Wallgren
    #!/usr/bin/env bash
    # SPDX-FileCopyrightText: 2019 Martin Wallgren
    #
    # SPDX-License-Identifier: MIT
    #
    # SPDX-FileContributor: Anthony Accioly <[email protected]>
    #
    # Changelog:
    # - 2025-06-16: Anthony Accioly - Added fzf preview, pbcopy support and default branch detection
    #
    # Original at: https://gist.github.com/MartinWallgren/5c00716d176350a842f46037a79f9230
    function usage() {
    echo "Git command to help you select which commit to create a fixup commit for."
    echo ""
    @@ -34,19 +41,31 @@ while [ ! $# -eq 0 ]; do
    shift
    done

    upstream=$(git rev-parse --abbrev-ref @{upstream} &>/dev/null)
    if [ -z "${upstream}" ]; then
    fixup_range="${GIT_DEFAULT_FIXUP_RANGE:-origin/master..HEAD}"
    upstream=$(git rev-parse --abbrev-ref "@{upstream}" &>/dev/null)
    # Determine the current branch and the default branch name dynamically.
    current_branch=$(git rev-parse --abbrev-ref HEAD)
    default_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
    if [ -z "$default_branch" ]; then
    default_branch="main"
    fi

    if [[ "${current_branch}" == "${default_branch}" ]]; then
    # If on the default branch, use all commits since root.
    fixup_range="$(git rev-list --max-parents=0 HEAD)..HEAD"
    elif [ -z "${upstream}" ]; then
    fixup_range="${GIT_DEFAULT_FIXUP_RANGE:-origin/${default_branch}..HEAD}"
    else
    fixup_range="${upstream}..HEAD"
    fi

    parent=$(git log --no-merges --oneline ${fixup_range} |
    fzf --height ${FZF_TMUX_HEIGHT:-40%} |
    parent=$(git log --no-merges --oneline "${fixup_range}" |
    fzf --height "${FZF_TMUX_HEIGHT:-40%}" \
    --preview "git show {1} --color=always" \
    --bind "ctrl-y:execute-silent(echo {1} | cut -d' ' -f1 | pbcopy)+abort" |
    cut -d' ' -f1)

    if [ -z "${parent}" ]; then
    echo "No commit found to fix."
    else
    git commit --fixup=${parent} "$@"
    git commit --fixup="${parent}" "$@"
    fi
  2. @MartinWallgren MartinWallgren revised this gist Jul 7, 2025. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions git-fixup
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    #!/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 ""
  3. @MartinWallgren MartinWallgren revised this gist Feb 26, 2019. 1 changed file with 10 additions and 9 deletions.
    19 changes: 10 additions & 9 deletions git-fixup
    Original file line number Diff line number Diff line change
    @@ -2,19 +2,20 @@
    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 to fix in the range from to the"
    echo "upstream to HEAD. If no upstream is set for the current branch,"
    echo "origin/master..HEAD will be used as the range to select from. The default"
    echo "range can be overriden with the environment variable GIT_DEFAULT_FIXUP_RANGE."
    echo ""
    echo "Example: GIT_DEFAULT_FIXUP_RANGE=origin/develop..HEAD git-fixup. Note that a"
    echo "configured upstream branch will take precedence over GIT_DEFAULT_FIXUP_RANGE."
    echo ""
    echo "The command will let you select a commit from the range and commit the current"
    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 ""
  4. @MartinWallgren MartinWallgren revised this gist Feb 26, 2019. 1 changed file with 16 additions and 17 deletions.
    33 changes: 16 additions & 17 deletions git-fixup
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    #!/bin/sh
    function usage()
    {
    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 to fix in the range from to the"
    @@ -21,28 +20,28 @@ function usage()
    echo ""
    }

    while [ ! $# -eq 0 ]
    do
    case "$1" in
    --help | -h)
    usage
    exit
    ;;
    esac
    shift
    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

    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)
    parent=$(git log --no-merges --oneline ${fixup_range} |
    fzf --height ${FZF_TMUX_HEIGHT:-40%} |
    cut -d' ' -f1)

    if [ -z "${parent}" ];then
    if [ -z "${parent}" ]; then
    echo "No commit found to fix."
    else
    git commit --fixup=${parent} "$@"
  5. @MartinWallgren MartinWallgren revised this gist Feb 26, 2019. 1 changed file with 41 additions and 4 deletions.
    45 changes: 41 additions & 4 deletions git-fixup
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,49 @@
    #!/bin/sh
    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 to fix in the range from to the"
    echo "upstream to HEAD. If no upstream is set for the current branch,"
    echo "origin/master..HEAD will be used as the range to select from. The default"
    echo "range can be overriden with the environment variable GIT_DEFAULT_FIXUP_RANGE."
    echo ""
    echo "Example: GIT_DEFAULT_FIXUP_RANGE=origin/develop..HEAD git-fixup. Note that a"
    echo "configured upstream branch will take precedence over GIT_DEFAULT_FIXUP_RANGE."
    echo ""
    echo "The command will let you select a commit from the 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 "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
    upstream=origin/master
    if [ -z "${upstream}" ];then
    fixup_range="${GIT_DEFAULT_FIXUP_RANGE:-origin/master..HEAD}"
    else
    fixup_range="${upstream}..HEAD"
    fi

    parent=$(git log --oneline ${upstream}..HEAD | \
    parent=$(git log --no-merges --oneline ${fixup_range} | \
    fzf --height ${FZF_TMUX_HEIGHT:-40%} | \
    cut -d' ' -f1)

    git commit --fixup=${parent} "$@"
    if [ -z "${parent}" ];then
    echo "No commit found to fix."
    else
    git commit --fixup=${parent} "$@"
    fi
  6. @MartinWallgren MartinWallgren revised this gist Feb 25, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-fixup
    Original file line number Diff line number Diff line change
    @@ -9,4 +9,4 @@ parent=$(git log --oneline ${upstream}..HEAD | \
    fzf --height ${FZF_TMUX_HEIGHT:-40%} | \
    cut -d' ' -f1)

    git commit --fixup=${parent}
    git commit --fixup=${parent} "$@"
  7. @MartinWallgren MartinWallgren created this gist Feb 25, 2019.
    12 changes: 12 additions & 0 deletions git-fixup
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    #!/bin/sh

    upstream=$(git rev-parse --abbrev-ref @{upstream} &> /dev/null)
    if [ -z ${upstream} ];then
    upstream=origin/master
    fi

    parent=$(git log --oneline ${upstream}..HEAD | \
    fzf --height ${FZF_TMUX_HEIGHT:-40%} | \
    cut -d' ' -f1)

    git commit --fixup=${parent}