Created
July 24, 2022 04:20
-
-
Save kawarimidoll/36430d3016d1c053594b6a69bde83f90 to your computer and use it in GitHub Desktop.
Smart git-switch using fzf
This file contains 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/bash | |
# Save this file as `git-sw` in your $PATH and run `chmod +x git-sw`. | |
# Run `git sw -` to switch to last branch. | |
# Run `git sw` to select branch using fzf. | |
# Run `git sw branch-name` to pass query to fzf. | |
# If the argument is matched to existing branch, change to that immediately. | |
# Create new branch when query is not matched by `fzf --print-query | tail -1`. | |
# ref: https://github.com/junegunn/fzf/issues/1693#issuecomment-699642792 | |
git switch "$@" 2>/dev/null || \ | |
git branch --sort=-authordate --all | grep --invert-match HEAD | \ | |
fzf --query="$@" --print-query --cycle --no-multi \ | |
--header-first --header='Create new branch when query is not matched' \ | |
--preview="echo {} | sed 's/.* //' | \ | |
xargs git log -30 --pretty=format:'[%ad] %s <%an>' --date=format:'%F'" | \ | |
tail -1 | \ | |
sed 's/.* //' | sed 's#remotes/[^/]*/##' | \ | |
xargs --no-run-if-empty -I_ sh -c 'git switch _ || git switch --create _' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment