git: Use switch instead of checkout if available
For git versions that support it, the aliases will use the newer `switch` instruction instead of `checkout` for its use cases. Creating a new branch through `gcb` is for example done by `git switch -c` instead of the older `git checkout -b`. Additionally streamlined git version checking to be a little faster and to unify its approach on posix sh, bash and zsh instead of utilizing individual checking functions.
This commit is contained in:
parent
cf85357f5c
commit
df0cf3d540
3 changed files with 21 additions and 30 deletions
|
@ -1,9 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
if ! exist git; then
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# while zsh can detect versions, bash
|
|
||||||
# in shells other than zsh, simply fall back to save
|
|
||||||
alias gsta='git stash save'
|
|
|
@ -4,7 +4,9 @@ if ! exist git; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# git alias
|
# print git version output and get raw version number by stripping prefix
|
||||||
|
git_version=$(git --version 2>/dev/null)
|
||||||
|
git_version="${git_version##git version }"
|
||||||
|
|
||||||
alias g='git'
|
alias g='git'
|
||||||
|
|
||||||
|
@ -18,10 +20,17 @@ alias gc='git commit -v'
|
||||||
alias gc!='git commit -v --amend'
|
alias gc!='git commit -v --amend'
|
||||||
alias gcn!='git commit -v --no-edit --amend'
|
alias gcn!='git commit -v --no-edit --amend'
|
||||||
|
|
||||||
alias gcm='git checkout master 2>/dev/null || git checkout main'
|
if version_at_least 2.23 "$git_version"; then
|
||||||
alias gcd='git checkout develop'
|
alias gcm='git switch master 2>/dev/null || git switch main'
|
||||||
alias gcb='git checkout -b'
|
alias gcd='git switch develop'
|
||||||
alias gco='git checkout'
|
alias gcb='git switch -c'
|
||||||
|
alias gco='git switch'
|
||||||
|
else
|
||||||
|
alias gcm='git checkout master 2>/dev/null || git checkout main'
|
||||||
|
alias gcd='git checkout develop'
|
||||||
|
alias gcb='git checkout -b'
|
||||||
|
alias gco='git checkout'
|
||||||
|
fi
|
||||||
|
|
||||||
alias gd='git diff'
|
alias gd='git diff'
|
||||||
alias gds='git diff --staged'
|
alias gds='git diff --staged'
|
||||||
|
@ -59,7 +68,13 @@ alias grbm='git rebase master || git rebase main'
|
||||||
|
|
||||||
alias gst='git status'
|
alias gst='git status'
|
||||||
|
|
||||||
# stash push/save is handled differently by zsh/bash
|
|
||||||
alias gstp='git stash pop'
|
alias gstp='git stash pop'
|
||||||
alias gstl='git stash list'
|
alias gstl='git stash list'
|
||||||
alias gstL='git stash list --stat'
|
alias gstL='git stash list --stat'
|
||||||
|
if version_at_least 2.13 "$git_version"; then
|
||||||
|
alias gsta='git stash push'
|
||||||
|
else
|
||||||
|
alias gsta='git stash save'
|
||||||
|
fi
|
||||||
|
|
||||||
|
unset -v git_version
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
#!/usr/bin/env zsh
|
|
||||||
# shellcheck shell=sh
|
|
||||||
|
|
||||||
if ! exist git; then
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# git alias
|
|
||||||
# if git is at least version 2.13, we can use git stash push
|
|
||||||
autoload -Uz is-at-least
|
|
||||||
if is-at-least 2.13 "$(git --version 2>/dev/null | awk '{print $3}')"; then
|
|
||||||
alias gsta='git stash push'
|
|
||||||
else
|
|
||||||
alias gsta='git stash save'
|
|
||||||
fi
|
|
Loading…
Reference in a new issue