Created
November 6, 2025 18:58
-
-
Save nafeu/191e5fe62a6da3d5cd04eb2420f74490 to your computer and use it in GitHub Desktop.
Zsh functions for interactive Git branch checkout (ico) and PR browsing (ipr) using fzf and GitHub CLI.
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
| # ----------------------------------------------------------------------------- | |
| # Git Helpers with fzf | |
| # ----------------------------------------------------------------------------- | |
| # Author: Nafeu Nasir | |
| # | |
| # Description: | |
| # A set of interactive shell functions to improve Git workflow using fzf | |
| # - `ico`: Quickly checkout local branches with a specific prefix, sorted | |
| # by last commit date. | |
| # - `ipr`: List your open GitHub PRs for the current repo and open the | |
| # selected one in the default web browser. | |
| # | |
| # Dependencies: | |
| # - git | |
| # - fzf (https://github.com/junegunn/fzf) | |
| # - GitHub CLI (gh) (https://cli.github.com/) | |
| # ----------------------------------------------------------------------------- | |
| # ----------------------------- | |
| # Configurable variables | |
| # ----------------------------- | |
| export GIT_PR_AUTHOR="your_github_username" # GitHub username for filtering PRs | |
| export GIT_BRANCH_PREFIX="example/prefix-" # Prefix for branch selection | |
| # ----------------------------- | |
| # Branch checkout function | |
| # ----------------------------- | |
| # Usage: ico | |
| # Lists local branches starting with $GIT_BRANCH_PREFIX, sorted by latest | |
| # commit date, then allows you to select one via fzf to checkout. | |
| ico() { | |
| # Ensure we're inside a git repository | |
| repo_root=$(git rev-parse --show-toplevel 2>/dev/null) || { | |
| echo "ico: not inside a git repository." >&2 | |
| return 1 | |
| } | |
| cd "$repo_root" || return 1 | |
| # List local branches matching prefix and select via fzf | |
| branch=$(git for-each-ref --sort=-committerdate \ | |
| --format="%(committerdate:short) %(refname:short)" refs/heads/ | \ | |
| grep " ${GIT_BRANCH_PREFIX}" | \ | |
| fzf --prompt="Checkout branch: " --no-sort --reverse | \ | |
| awk '{print $2}') | |
| # Checkout selected branch | |
| if [ -n "$branch" ]; then | |
| git checkout "$branch" | |
| else | |
| echo "No branch selected." | |
| fi | |
| } | |
| # ----------------------------- | |
| # GitHub PR opener function | |
| # ----------------------------- | |
| # Usage: ipr | |
| # Lists your open PRs for the current repository and opens the selected | |
| # one in the default web browser. | |
| ipr() { | |
| # Check for GitHub CLI | |
| if ! command -v gh >/dev/null 2>&1; then | |
| echo "gh CLI is not installed or not in PATH." >&2 | |
| return 1 | |
| fi | |
| pr_number=$(gh pr list --state open --author "$GIT_PR_AUTHOR" --json number,title \ | |
| --template "{{range .}}{{.number}} {{.title}} | |
| {{end}}" | fzf --prompt="Open PR: " --no-sort --reverse | awk '{print $1}') | |
| if [ -n "$pr_number" ]; then | |
| gh pr view "$pr_number" --web | |
| else | |
| echo "No PR selected." | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment