:question: Is there a way to revert a whole (merge...
# how-do-i
z
❓ Is there a way to revert a whole (merged) Graphite stack?
p
(If applicable) To help us more clearly understand what you're trying to do, please provide additional details about: - Screenshots / debug logs / list of commands you've tried to run so far - Your workflow - Current state
Hi Zeshan, there is not a graphite specific way to do this, you would need toΒ  revert the merged PRs in reverse order.
z
Got it, thanks! I'm sure you can see how that's pretty frustrating in an incident scenario etc – please could you add a "revert entire stack" feature to your customer feature requests list for me? πŸ™
πŸ‘ 1
m
+1 - in general get the git hashes for a list or merged PRs is annoying which also makes reverting, etc. cumbersome. I wrote myself a small function to help a bit at least so you only have to search for one sha of the first (last?) merged PR and it prints you a list with the sha and the PR number and title. I'm sure you can modify the commands so they print in reverse order and team it up with `git revert`:
Copy code
# Fish function to display git log with clickable GitHub PR links.
#
# Usage: git-pr-log [git-log-options]
# Example: git-pr-log -n 10 <sha>

function git-pr-log
    # 1. Get the repository path (e.g., "owner/repo") from the 'origin' remote URL.
    # The 'set -l' command creates a local variable.
    # Command substitutions use parentheses: (command).
    # Stderr is redirected with ^/dev/null.
    set -l repo_url (git remote get-url origin)

    # Check if the variable is empty.
    if test -z "$repo_url"
        echo "Error: Could not find 'origin' remote. Are you in a git repository?" >&2
        return 1
    end

    # Use sed to extract "owner/repo" from the URL.
    set -l repo_path (echo "$repo_url" | sed -e 's/.*<http://github.com[:\/]//|github.com[:\/]//>' -e 's/\.git$//')

    # 2. Process the git log output commit by commit.
    #    In Fish, '$argv' contains all arguments passed to the function.
    #    The 'while read' loop is similar to bash.
    git log --format='%H' $argv | while read -l sha
        # Get details for the specific commit.
        set -l short_sha (git show -s --format='%h' $sha)
        set -l message (git show -s --format='%s %b' $sha) # Get subject and body

        # 3. Try to find the PR number.
        #    The pipeline of external commands (echo, grep, head) works the same.
        #    First, check for the squash-merge format, e.g., "(#123)"
        set -l pr_number (echo "$message" | grep -o '(#\([0-9]\+\))' | head -n 1 | grep -o '[0-9]\+')

        # If not found, check for the merge-commit format, e.g., "Merge pull request #123"
        if test -z "$pr_number"
            set -l pr_number (echo "$message" | grep -o 'Merge pull request #[0-9]\+' | head -n 1 | grep -o '[0-9]\+')
        end

        # 4. Print the formatted output.
        if test -n "$pr_number"
            # If a PR number was found, print the link.
            set -l pr_link "<https://github.com/$repo_path/pull/$pr_number>"
            set -l commit_title (git show -s --format='%s' $sha | sed 's/ *(#[0-9]\+)$//')

            # Fish has built-in helpers for color, which is more readable than ANSI codes.
            printf "%s [#%s](%s) -- %s\n" $short_sha $pr_number $pr_link $commit_title
        else
            # Fallback: If no PR number, just print the standard oneline format.
            git show -s --format='%h %s' $sha
        end
    end
end