Skip to content

Instantly share code, notes, and snippets.

@EspenBrun
Created November 8, 2024 15:18
Show Gist options
  • Save EspenBrun/d5a688639261ddf46d33d9ba502d3b40 to your computer and use it in GitHub Desktop.
Save EspenBrun/d5a688639261ddf46d33d9ba502d3b40 to your computer and use it in GitHub Desktop.
########################
# Unalias to make functions priority
########################
# unalias -m '*'
unalias -m 'gr'
unalias -m 'gl'
unalias -m 'glp'
unalias -m 'gdi'
unalias -m 'gra'
unalias -m 'gaa'
unalias -m 'gp'
unalias -m 'gcmsg'
unalias -m 'gcp'
unalias -m 'gfc'
unalias -m 'follow'
unalias -m 'gai'
unalias -m 'gapi'
unalias -m 'gci'
unalias -m 'gpr'
unalias -m 'gbd'
unalias -m 'nrb'
unalias -m 'grf'
########################
# Shell specific
########################
alias reload='exec zsh'
alias gs='git status'
alias gf='git fetch'
alias ga='git add'
alias ga.='git add .'
alias gc='git checkout'
alias gc.='git checkout .'
alias gb='git --no-pager branch'
alias gcb='git checkout -b'
alias gm='git merge'
alias gd='git --no-pager diff' # show whole diff in terminal
alias gcm='git commit -m'
alias gcam='git add -A && git commit -m'
alias gcmc='git commit -m "Cleaning"'
alias gcamc='git add -A && git commit -m "Cleaning"'
alias gcf='git commit --fixup $(git log --pretty=oneline | fzf | cut -d " " -f 1)'
alias gps='git push'
alias gpl='git pull'
alias stash='git stash'
alias apply='git stash apply'
alias pop='git stash pop'
alias amend='git commit --amend'
alias amendne='git commit --amend --no-edit'
alias amenda='git add . && git commit --amend --no-edit'
alias mstr='git checkout master'
alias dev='git checkout develop'
alias hard='git reset --hard'
alias gr='git rebase'
alias grm='git pull --rebase origin master'
alias grd='git pull --rebase origin develop'
alias grr='git pull --rebase origin `git rev-parse --abbrev-ref HEAD`'
alias gra='git rebase --abort'
alias grc='git rebase --continue'
alias dry='git remote prune origin --dry-run && echo "would delete these local branches:" && git branch --merged | egrep -v "(^\*|master|development|develop|release*)"'
alias prune='git checkout master && git pull && git remote prune origin && git branch --merged | egrep -v "(^\*|master|development|develop|release*)" | xargs git branch -d'
alias copy='git branch -f copy && echo "Created branch copy"'
alias gcp='git cherry-pick'
alias gcpa='git cherry-pick --abort'
alias gcpc='git cherry-pick --continue'
alias parent='git show-branch -a | grep "\*" | grep -v `git rev-parse --abbrev-ref HEAD` | head -n1 | sed "s/.*\[\(.*\)\].*/\1/" | sed "s/[\^~].*//"'
alias glg='git --no-pager log -i --grep '
alias gbsc='git branch --show-current'
alias branch='git rev-parse --abbrev-ref HEAD'
########################
# Internal functions
########################
function _checkForFail() {
if [ ! $? -eq 0 ]; then
exit 1
fi
}
########################
# Git
########################
# Aliases #
# Print git log pretty oneline short commit
function gl(){
amount=${1:-20}
git --no-pager -c color.ui=always log --pretty=format:'%C(yellow)%h|%C(magenta)%ad|%Cblue%an|%Cgreen%d %Creset%s' --date=short -$amount| column -ts'|'
}
# Print git log pretty oneline affected files
function glf(){
amount=${1:-10}
git --no-pager -c color.ui=always log --pretty=format:'%C(yellow)%H | %C(magenta)%ad | %Cblue%an | %Cgreen%d %Creset%s' --date=format:'%Y-%m-%d %H:%M:%S' --stat -$amount
}
# Print git log pretty oneline short commit affected files
function glfs(){
amount=${1:-10}
git --no-pager -c color.ui=always log --pretty=format:'%C(yellow)%h | %C(magenta)%ad | %Cblue%an | %Cgreen%d %Creset%s' --date=format:'%Y-%m-%d %H:%M:%S' --stat -$amount
}
# Show latest or given commit
function show(){
commit=${1:-}
if [ -z "$commit" ]
then
git --no-pager show
else
if [ "${#commit}" -gt 8 ]
then
git --no-pager diff $commit^!
else
git --no-pager diff @~$commit^!
fi
fi
}
# Checkout commit interactive
function gci(){
git checkout $(git log --pretty=oneline | fzf | cut -d " " -f 1)
}
# Show interactive
function showi(){
show $(git log --pretty=oneline | fzf | cut -d " " -f 1)
}
# Show files in latest or given commit
function showf(){
commit=${1:-HEAD}
git diff-tree --no-commit-id --name-only -r $commit
}
# Add files interactively
function gai(){
# --porcelain is for human readable output
# use awk to not include first column (??, M, etc), and give output as one line separated with space
# use awk to surround with ", for untracked files. Also surrounds tracked files with ""
# use sed to replace "" with ", for files already tracked
# use xarg to pipe into new command
# can be used like gai -p to patch files as well!
git status --porcelain | fzf -m | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' | awk '{ printf "\"%s\" ", $0 }' | sed "s/\"\"/\"/g" | xargs git add
}
function gapi(){
git status --porcelain | fzf -m | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' | awk '{ printf "\"%s\" ", $0 }' | sed "s/\"\"/\"/g" | xargs git add -p
}
# Add daily note with commit message
function gcdn(){
filepath=$(git status --porcelain | fzf -m | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' | awk '{ printf "\"%s\" ", $0 }' | sed "s/\"\"//g" | sed "s/ /\\ /g")
filepath=${filepath%?}
file=${filepath##*/}
file=${file%???}
echo $filepath
echo $file
git add $filepath && git commit -m "$file"
}
# Show what commits a file was in, defaults to ten commits
function follow(){
: "${1?Missing file}"
amount=${2:-10}
file=$1
git --no-pager log -$amount --follow $file
}
# Show what commits a file was in, defaults to ten commits
function followi(){
amount=${1:-10}
# file=$(git status --porcelain | fzf | awk '{ print $2 }')
# git --no-pager log -$amount --follow $file
git status --porcelain | fzf -m | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' | awk '{ printf "\"%s\" ", $0 }' | sed "s/\"\"/\"/g" | xargs git --no-pager log -$amount --follow
}
# Diff selected file
function gdi(){
git status --porcelain | fzf -m | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' | awk '{ printf "\"%s\" ", $0 }' | sed "s/\"\"/\"/g" | xargs git --no-pager diff
}
# Show difference in commits between HEAD and given commit
function gdc(){
commit=${1:-master}
git -c color.ui=always log --pretty=format:'%C(yellow)%h|%C(magenta)%ad|%Cblue%an|%Cgreen%d %Creset%s' --date=format:'%Y-%m-%d %H:%M:%S' --abbrev-commit $commit..HEA| column -ts'|'
}
# Rebase on commit, handy for amending earlier commits or to squash commits
# Argument: Base to rebase on top of
function gri(){
commit=${1:-}
if [ -z "$commit" ]
then
git rebase -i --autosquash $(git log --abbrev-commit --pretty=oneline | fzf | cut -d " " -f 1)
else
git rebase -i --autosquash $commit
fi
}
# Checkout first branch that has the given number in it by grepping
function gcg(){
: "${1?Missing issue number}"
issue=$1
git checkout $(git branch | grep $issue | sed -n 1p)
}
# Checkout first remote branch that has the given number in it by grepping
function gfc(){
: "${1?Missing issue number}"
issue=$1
git fetch
git checkout $(git branch -r | grep $issue | sed -n 1p | cut -c 10-)
}
# Soft reset of commits
function soft(){
number=${1:-1}
git reset --soft head~$number && git reset
}
function hardorigin(){
branch=$(git rev-parse --abbrev-ref HEAD)
git fetch && git reset --hard origin/$branch
}
function isrebased(){
if git merge-base --is-ancestor origin/master HEAD; then
echo "Commit at HEAD is an ancestor of origin/master."
else
echo "Commit at HEAD is not an ancestor of origin/master."
exit 1 # Exit with non-zero status to indicate failure
fi
}
# Takes staged files and adds them to the desired commit
# Argument is some text in the commit message you want the changes to be added to
function grf(){
commit=${1:-}
if [ -z "$commit" ]
then
commit=$(git log --pretty=oneline | fzf | cut -d " " -f 1)
fi
git commit --fixup $commit
echo "Attempting to rebase and fixup into commit $commit"
git --no-pager log -n 1 --pretty=format:%s $commit
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash $commit^
}
# Functions
# Remove files selected from git status
function rmi(){
toBeRemoved=$(git status --porcelain | fzf -m | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' | awk '{ printf "\"%s\" ", $0 }' | sed "s/\"\"/\"/g")
echo "Removing $toBeRemoved"
echo $toBeRemoved | xargs rm
}
# Cat file contents from git status
function cati(){
# --porcelain is for human readable output
# use awk to not include first column (??, M, etc), and give output as one line separated with space
# use awk to surround with ", for untracked files. Also surrounds tracked files with ""
# use sed to replace "" with ", for files already tracked
# use xarg to pipe into new command
# can be used like gai -p to patch files as well!
git status --porcelain | fzf -m | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}' | awk '{ printf "\"%s\" ", $0 }' | sed "s/\"\"/\"/g" | xargs cat
}
function mkdircd(){
: "${1?Missing directory name}"
name=$1
mkdir $name
cd $name
}
function setdate(){
# "mm/dd/[yy]yy [hh:mm:[:ss] [AM | PM]]"
: "${1?Missing date}"
: "${2?Missing filename}"
new_date=$1
file_name=$2
setfile -d $new_date -m $new_date $file_name
}
# fzf
export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment