Last active
December 1, 2024 18:37
-
-
Save vgaidarji/8c393dfb90efee44d3d3a5d3bb3adf5b to your computer and use it in GitHub Desktop.
[Windows] ~/.bashrc with ssh-agent and zsh git plugin port
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
env=~/.ssh/agent.env | |
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; } | |
agent_start () { | |
(umask 077; ssh-agent >| "$env") | |
. "$env" >| /dev/null ; } | |
agent_load_env | |
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2=agent not running | |
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?) | |
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then | |
agent_start | |
ssh-add /c/Users/user/.ssh/veaceslav.gaidarji | |
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then | |
ssh-add /c/Users/user/.ssh/veaceslav.gaidarji | |
fi | |
unset env | |
source ~/.zsh-git-plugin-port-for-windows |
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
# zsh git plugin port for windows | |
# https://github.com/ohmyzsh/ohmyzsh/blob/69a6359f7cf8978d464573fb7b023ee3cd00181a/plugins/git/git.plugin.zsh | |
# | |
# Git version checking | |
git_version=$(git version 2>/dev/null | awk '{print $3}') | |
# The name of the current branch | |
# Back-compatibility wrapper for when this function was defined here in | |
# the plugin, before being pulled in to core lib/git.zsh as git_current_branch() | |
# to fix the core -> git plugin dependency. | |
current_branch() { | |
git_current_branch | |
} | |
# Function to get the current branch name in Git | |
git_current_branch() { | |
git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD | |
} | |
# Check for develop and similarly named branches | |
git_develop_branch() { | |
git rev-parse --git-dir &>/dev/null || return | |
local branch | |
for branch in dev devel develop development; do | |
if git show-ref -q --verify refs/heads/$branch; then | |
echo "$branch" | |
return 0 | |
fi | |
done | |
echo "develop" | |
return 1 | |
} | |
# Check if main exists and use instead of master | |
git_main_branch() { | |
git rev-parse --git-dir &>/dev/null || return | |
local ref | |
for ref in refs/heads/main refs/heads/trunk refs/heads/mainline refs/heads/default refs/heads/stable refs/heads/master \ | |
refs/remotes/origin/main refs/remotes/origin/trunk refs/remotes/origin/mainline refs/remotes/origin/default refs/remotes/origin/stable refs/remotes/origin/master \ | |
refs/remotes/upstream/main refs/remotes/upstream/trunk refs/remotes/upstream/mainline refs/remotes/upstream/default refs/remotes/upstream/stable refs/remotes/upstream/master; do | |
if git show-ref -q --verify "$ref"; then | |
echo "${ref##*/}" | |
return 0 | |
fi | |
done | |
# If no main branch was found, fall back to master but return error | |
echo "master" | |
return 1 | |
} | |
grename() { | |
if [ -z "$1" ] || [ -z "$2" ]; then | |
echo "Usage: $FUNCNAME old_branch new_branch" | |
return 1 | |
fi | |
# Rename branch locally | |
git branch -m "$1" "$2" | |
# Rename branch in origin remote | |
if git push origin :"$1"; then | |
git push --set-upstream origin "$2" | |
fi | |
} | |
# Similar to `gunwip` but recursive "Unwips" all recent `--wip--` commits not just the last one | |
gunwipall() { | |
local _commit | |
_commit=$(git log --grep='--wip--' --invert-grep --max-count=1 --format=format:%H) | |
# Check if a commit without "--wip--" was found and it's not the same as HEAD | |
if [ "$_commit" != "$(git rev-parse HEAD)" ]; then | |
git reset "$_commit" || return 1 | |
fi | |
} | |
# Warn if the current branch is a WIP | |
work_in_progress() { | |
git -c log.showSignature=false log -n 1 2>/dev/null | grep -q -- "--wip--" && echo "WIP!!" | |
} | |
# | |
# Aliases | |
# (sorted alphabetically by command) | |
# (order should follow README) | |
# (in some cases force the alias order to match README, like for example gke and gk) | |
# | |
alias grt='cd "$(git rev-parse --show-toplevel || echo .)"' | |
ggpnp() { | |
if [ "$#" -eq 0 ]; then | |
ggl && ggp | |
else | |
ggl "$@" && ggp "$@" | |
fi | |
} | |
complete -o default -o nospace -F _git ggpnp | |
alias ggpur='ggu' | |
alias g='git' | |
alias ga='git add' | |
alias gaa='git add --all' | |
alias gapa='git add --patch' | |
alias gau='git add --update' | |
alias gav='git add --verbose' | |
alias gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify --no-gpg-sign --message "--wip-- [skip ci]"' | |
alias gam='git am' | |
alias gama='git am --abort' | |
alias gamc='git am --continue' | |
alias gamscp='git am --show-current-patch' | |
alias gams='git am --skip' | |
alias gap='git apply' | |
alias gapt='git apply --3way' | |
alias gbs='git bisect' | |
alias gbsb='git bisect bad' | |
alias gbsg='git bisect good' | |
alias gbsn='git bisect new' | |
alias gbso='git bisect old' | |
alias gbsr='git bisect reset' | |
alias gbss='git bisect start' | |
alias gbl='git blame -w' | |
alias gb='git branch' | |
alias gba='git branch --all' | |
alias gbd='git branch --delete' | |
alias gbD='git branch --delete --force' | |
gbda() { | |
git branch --no-color --merged | grep -vE "^([+*]|\s*($(git_main_branch)|$(git_develop_branch))\s*$)" | xargs git branch --delete 2>/dev/null | |
} | |
# Copied and modified from James Roeder (jmaroeder) under MIT License | |
# https://github.com/jmaroeder/plugin-git/blob/216723ef4f9e8dde399661c39c80bdf73f4076c4/functions/gbda.fish | |
gbds() { | |
local default_branch | |
# Get the default branch (either main or develop) | |
default_branch=$(git_main_branch) | |
if [ $? -ne 0 ]; then | |
default_branch=$(git_develop_branch) | |
fi | |
# Check if a default branch was found | |
if [ -z "$default_branch" ]; then | |
echo "No default branch found. Aborting." | |
return 1 | |
fi | |
# Loop through all local branches and delete those that are no longer needed | |
git for-each-ref refs/heads/ "--format=%(refname:short)" | \ | |
while read -r branch; do | |
local merge_base | |
# Get the merge base for the branch | |
merge_base=$(git merge-base "$default_branch" "$branch") | |
# Compare the branches using git cherry and delete the branch if necessary | |
cherry_output=$(git cherry "$default_branch" "$(git commit-tree "$(git rev-parse "$branch^"{tree})" -p "$merge_base" -m _)") | |
# Check if cherry_output starts with a negative sign, indicating branch should be deleted | |
if [[ "$cherry_output" =~ ^- ]]; then | |
echo "Deleting branch: $branch" | |
git branch -D "$branch" | |
fi | |
done | |
} | |
gbgd() { | |
LANG=C git branch --no-color -vv | grep ": gone]" | \ | |
cut -c 3- | awk '{print $1}' | xargs -I {} git branch -d {} | |
} | |
gbgD() { | |
LANG=C git branch --no-color -vv | grep ": gone]" | \ | |
cut -c 3- | awk '{print $1}' | xargs -I {} git branch -D {} | |
} | |
alias gbm='git branch --move' | |
alias gbnm='git branch --no-merged' | |
alias gbr='git branch --remote' | |
alias ggsup='git branch --set-upstream-to=origin/$(git_current_branch)' | |
alias gbg='LANG=C git branch -vv | grep ": gone\]"' | |
alias gco='git checkout' | |
alias gcor='git checkout --recurse-submodules' | |
alias gcb='git checkout -b' | |
alias gcB='git checkout -B' | |
alias gcd='git checkout $(git_develop_branch)' | |
alias gcm='git checkout $(git_main_branch)' | |
alias gcp='git cherry-pick' | |
alias gcpa='git cherry-pick --abort' | |
alias gcpc='git cherry-pick --continue' | |
alias gclean='git clean --interactive -d' | |
alias gcl='git clone --recurse-submodules' | |
alias gclf='git clone --recursive --shallow-submodules --filter=blob:none --also-filter-submodules' | |
gccd() { | |
# get repo URI from args based on valid formats: https://git-scm.com/docs/git-clone#URLS | |
local repo | |
repo=$(for arg in "$@"; do | |
if [[ "$arg" =~ ^(ssh://|git://|ftp://|ftps://|http://|https://|.+@.+)(.+\.git)?$ ]]; then | |
echo "$arg" | |
fi | |
done | tail -n 1) | |
# Default to last argument if no valid repo URI was found | |
repo="${repo:-$_}" | |
# Clone repository and exit if it fails | |
git clone --recurse-submodules "$@" || return | |
# If last argument was a directory, cd into it, otherwise use the repo name | |
if [ -d "$_" ]; then | |
cd "$_" || return | |
else | |
cd "${repo##*/}" && cd "${PWD%.git}" || return | |
fi | |
} | |
complete -o default -o nospace -F _git gccd | |
alias gcam='git commit --all --message' | |
alias gcas='git commit --all --signoff' | |
alias gcasm='git commit --all --signoff --message' | |
alias gcs='git commit --gpg-sign' | |
alias gcss='git commit --gpg-sign --signoff' | |
alias gcssm='git commit --gpg-sign --signoff --message' | |
alias gcmsg='git commit --message' | |
alias gcsm='git commit --signoff --message' | |
alias gc='git commit --verbose' | |
alias gca='git commit --verbose --all' | |
alias gca!='git commit --verbose --all --amend' | |
alias gcan!='git commit --verbose --all --no-edit --amend' | |
alias gcans!='git commit --verbose --all --signoff --no-edit --amend' | |
alias gcann!='git commit --verbose --all --date=now --no-edit --amend' | |
alias gc!='git commit --verbose --amend' | |
alias gcn='git commit --verbose --no-edit' | |
alias gcn!='git commit --verbose --no-edit --amend' | |
alias gcf='git config --list' | |
alias gdct='git describe --tags $(git rev-list --tags --max-count=1)' | |
alias gd='git diff' | |
alias gdca='git diff --cached' | |
alias gdcw='git diff --cached --word-diff' | |
alias gds='git diff --staged' | |
alias gdw='git diff --word-diff' | |
gdv() { | |
git diff -w "$@" | less | |
} | |
# Note: The `compdef` directive is Zsh-specific and does not work in Bash. | |
# You can add autocompletion in Bash using `complete` if needed: | |
# complete -o default -o nospace -F _git gdv | |
alias gdup='git diff @{upstream}' | |
gdnolock() { | |
git diff "$@" ":(exclude)package-lock.json" ":(exclude)*.lock" | |
} | |
complete -o default -o nospace -F _git gdv | |
complete -o default -o nospace -F _git gdnolock | |
alias gdt='git diff-tree --no-commit-id --name-only -r' | |
alias gf='git fetch' | |
# Get Git version (e.g., 2.8.0) | |
git_version=$(git --version | awk '{print $3}') | |
# Compare Git version with 2.8 using awk | |
git_version_numeric=$(echo "$git_version" | awk -F. '{print $1*10000 + $2*100 + $3}') | |
# Compare the numeric value of the git version with 2.8 | |
min_version=20800 # 2.8.0 as a numeric value | |
if [ "$git_version_numeric" -ge "$min_version" ]; then | |
# If git version is >= 2.8, use --jobs=10 for git fetch | |
alias gfa='git fetch --all --tags --prune --jobs=10' | |
else | |
# Otherwise, use the older version of git fetch | |
alias gfa='git fetch --all --tags --prune' | |
fi | |
alias gfo='git fetch origin' | |
alias gg='git gui citool' | |
alias gga='git gui citool --amend' | |
alias ghh='git help' | |
alias glgg='git log --graph' | |
alias glgga='git log --graph --decorate --all' | |
alias glgm='git log --graph --max-count=10' | |
alias glods='git log --graph --pretty="%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset" --date=short' | |
alias glod='git log --graph --pretty="%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset"' | |
alias glola='git log --graph --pretty="%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset" --all' | |
alias glols='git log --graph --pretty="%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset" --stat' | |
alias glol='git log --graph --pretty="%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset"' | |
alias glo='git log --oneline --decorate' | |
alias glog='git log --oneline --decorate --graph' | |
alias gloga='git log --oneline --decorate --graph --all' | |
# Pretty log messages | |
_git_log_prettily() { | |
if [ ! -z "$1" ]; then | |
git log --pretty="$1" | |
fi | |
} | |
complete -o default -o nospace -F _git _git_log_prettily | |
alias glp='_git_log_prettily' | |
alias glg='git log --stat' | |
alias glgp='git log --stat --patch' | |
alias gignored='git ls-files -v | grep "^[[:lower:]]"' | |
alias gfg='git ls-files | grep' | |
alias gm='git merge' | |
alias gma='git merge --abort' | |
alias gmc='git merge --continue' | |
alias gms="git merge --squash" | |
alias gmff="git merge --ff-only" | |
alias gmom='git merge origin/$(git_main_branch)' | |
alias gmum='git merge upstream/$(git_main_branch)' | |
alias gmtl='git mergetool --no-prompt' | |
alias gmtlvim='git mergetool --no-prompt --tool=vimdiff' | |
alias gl='git pull' | |
alias gpr='git pull --rebase' | |
alias gprv='git pull --rebase -v' | |
alias gpra='git pull --rebase --autostash' | |
alias gprav='git pull --rebase --autostash -v' | |
# Function to pull the latest changes with rebase | |
ggu() { | |
if [ "$#" != 1 ]; then | |
local b="$(git rev-parse --abbrev-ref HEAD)" # Equivalent to git_current_branch in Bash | |
fi | |
git pull --rebase origin "${b:=$1}" | |
} | |
complete -o default -o nospace -F _git ggu | |
alias gprom='git pull --rebase origin $(git_main_branch)' | |
alias gpromi='git pull --rebase=interactive origin $(git_main_branch)' | |
alias gprum='git pull --rebase upstream $(git_main_branch)' | |
alias gprumi='git pull --rebase=interactive upstream $(git_main_branch)' | |
alias ggpull='git pull origin "$(git_current_branch)"' | |
# Function to pull changes with optional branch specification | |
ggl() { | |
if [ "$#" != 0 ] && [ "$#" != 1 ]; then | |
git pull origin "${*}" | |
else | |
if [ "$#" == 0 ]; then | |
local b="$(git rev-parse --abbrev-ref HEAD)" # Equivalent to git_current_branch in Bash | |
fi | |
git pull origin "${b:=$1}" | |
fi | |
} | |
complete -o default -o nospace -F _git ggl | |
alias gluc='git pull upstream $(git_current_branch)' | |
alias glum='git pull upstream $(git_main_branch)' | |
alias gp='git push' | |
alias gpd='git push --dry-run' | |
# Function to force push the current or specified branch | |
ggf() { | |
if [ "$#" != 1 ]; then | |
local b="$(git rev-parse --abbrev-ref HEAD)" # Equivalent to git_current_branch in Bash | |
fi | |
git push --force origin "${b:=$1}" | |
} | |
complete -o default -o nospace -F _git ggf | |
# Force push with fallback for different Git versions | |
alias gpf='git push --force' | |
# Check for Git version >= 2.30 and set alias accordingly | |
git_version=$(git --version | awk '{print $3}') | |
if [[ $(echo "$git_version 2.30" | awk '{if ($1 >= $2) print 1; else print 0}') == 1 ]]; then | |
alias gpf='git push --force-with-lease --force-if-includes' | |
else | |
alias gpf='git push --force-with-lease' | |
fi | |
# Function to force push with lease for the current or specified branch | |
ggfl() { | |
if [ "$#" != 1 ]; then | |
local b="$(git rev-parse --abbrev-ref HEAD)" # Equivalent to git_current_branch in Bash | |
fi | |
git push --force-with-lease origin "${b:=$1}" | |
} | |
complete -o default -o nospace -F _git ggfl | |
# Set upstream and push for the current branch | |
alias gpsup='git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD)' | |
# Check for Git version >= 2.30 and set alias accordingly | |
git_version=$(git --version | awk '{print $3}') | |
if [[ $(echo "$git_version 2.30" | awk '{if ($1 >= $2) print 1; else print 0}') == 1 ]]; then | |
alias gpsupf='git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD) --force-with-lease --force-if-includes' | |
else | |
alias gpsupf='git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD) --force-with-lease' | |
fi | |
# Other aliases for Git push | |
alias gpv='git push --verbose' | |
alias gpoat='git push origin --all && git push origin --tags' | |
alias gpod='git push origin --delete' | |
# Push current branch to origin | |
alias ggpush='git push origin "$(git rev-parse --abbrev-ref HEAD)"' | |
# Function to push to the origin for the current or specified branch | |
ggp() { | |
if [ "$#" -ne 0 ] && [ "$#" -ne 1 ]; then | |
git push origin "$@" | |
else | |
if [ "$#" -eq 0 ]; then | |
local b="$(git rev-parse --abbrev-ref HEAD)" # Equivalent to git_current_branch in Bash | |
fi | |
git push origin "${b:=$1}" | |
fi | |
} | |
complete -o default -o nospace -F _git ggp | |
alias gpu='git push upstream' | |
alias grb='git rebase' | |
alias grba='git rebase --abort' | |
alias grbc='git rebase --continue' | |
alias grbi='git rebase --interactive' | |
alias grbo='git rebase --onto' | |
alias grbs='git rebase --skip' | |
alias grbd='git rebase $(git_develop_branch)' | |
alias grbm='git rebase $(git_main_branch)' | |
alias grbom='git rebase origin/$(git_main_branch)' | |
alias grbum='git rebase upstream/$(git_main_branch)' | |
alias grf='git reflog' | |
alias gr='git remote' | |
alias grv='git remote --verbose' | |
alias gra='git remote add' | |
alias grrm='git remote remove' | |
alias grmv='git remote rename' | |
alias grset='git remote set-url' | |
alias grup='git remote update' | |
alias grh='git reset' | |
alias gru='git reset --' | |
alias grhh='git reset --hard' | |
alias grhk='git reset --keep' | |
alias grhs='git reset --soft' | |
alias gpristine='git reset --hard && git clean --force -dfx' | |
alias gwipe='git reset --hard && git clean --force -df' | |
alias groh='git reset origin/$(git_current_branch) --hard' | |
alias grs='git restore' | |
alias grss='git restore --source' | |
alias grst='git restore --staged' | |
alias gunwip='git rev-list --max-count=1 --format="%s" HEAD | grep -q "\--wip--" && git reset HEAD~1' | |
alias grev='git revert' | |
alias greva='git revert --abort' | |
alias grevc='git revert --continue' | |
alias grm='git rm' | |
alias grmc='git rm --cached' | |
alias gcount='git shortlog --summary --numbered' | |
alias gsh='git show' | |
alias gsps='git show --pretty=short --show-signature' | |
alias gstall='git stash --all' | |
alias gstaa='git stash apply' | |
alias gstc='git stash clear' | |
alias gstd='git stash drop' | |
alias gstl='git stash list' | |
alias gstp='git stash pop' | |
# Check for Git version >= 2.13 and set alias for git stash | |
git_version=$(git --version | awk '{print $3}') | |
if [[ $(echo "$git_version 2.13" | awk '{if ($1 >= $2) print 1; else print 0}') == 1 ]]; then | |
alias gsta='git stash push' | |
else | |
alias gsta='git stash save' | |
fi | |
alias gsts='git stash show --patch' | |
alias gst='git status' | |
alias gss='git status --short' | |
alias gsb='git status --short --branch' | |
alias gsi='git submodule init' | |
alias gsu='git submodule update' | |
alias gsd='git svn dcommit' | |
alias git-svn-dcommit-push='git svn dcommit && git push github $(git_main_branch):svntrunk' | |
alias gsr='git svn rebase' | |
alias gsw='git switch' | |
alias gswc='git switch --create' | |
alias gswd='git switch $(git_develop_branch)' | |
alias gswm='git switch $(git_main_branch)' | |
alias gta='git tag --annotate' | |
alias gts='git tag --sign' | |
alias gtv='git tag | sort -V' | |
alias gignore='git update-index --assume-unchanged' | |
alias gunignore='git update-index --no-assume-unchanged' | |
alias gwch='git whatchanged -p --abbrev-commit --pretty=medium' | |
alias gwt='git worktree' | |
alias gwta='git worktree add' | |
alias gwtls='git worktree list' | |
alias gwtmv='git worktree move' | |
alias gwtrm='git worktree remove' | |
alias gstu='gsta --include-untracked' | |
# Define the function for listing Git tags | |
gtl() { | |
git tag --sort=-v:refname -n --list "${1}*" | |
} | |
# Create an alias to invoke the function, preventing globbing | |
alias gtl='noglob gtl' | |
alias gk='\gitk --all --branches &!' | |
alias gke='\gitk --all $(git log --walk-reflogs --pretty=%h) &!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment