Skip to content

Instantly share code, notes, and snippets.

@jonjitsu
Last active November 16, 2023 15:31
Show Gist options
  • Save jonjitsu/a5ad6338abdd32e5bd27d025bffb3250 to your computer and use it in GitHub Desktop.
Save jonjitsu/a5ad6338abdd32e5bd27d025bffb3250 to your computer and use it in GitHub Desktop.
.bash.d/git
# -*- mode: sh -*-
alias gs='git status '
alias gsp='git status --porcelain'
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias gk='gitk --all&'
alias gx='gitx --all'
alias got='git '
alias get='git '
git.commit() {
local target='--all'
[[ -n $1 ]] && target=$1
git add $target
git difftool -y --cached
git commit
}
# add + commit + push
# $1 [optional] git add target
git.push() {
git.commit "$@"
git remote | xargs -L1 git push
}
git.graph() {
git log --graph --decorate --oneline --branches --all
}
# $1 what to merge
git.merge.dry-run() {
git merge --no-commit --no-ff "$1"
git status
echo git merge --abort
}
git.merge.dry-run.abort() {
git merge --abort
}
git.merge.dry-run.push() {
git.commit.difftool \
&& git commit \
&& git push
}
git.commit.difftool() {
git difftool -y --cached
}
git.history() {
local file="$1"
git log -p "$file"
}
git.history.succint() {
local file="$1"
git log --pretty=oneline "$file"
}
git.history.commit-ids() {
local file="$1" args=()
local fn="git log --pretty=format:\"%H\""
if [[ -n $file ]]
then args+=(-- "$file")
fi
git log --full-history --pretty=format:"%H" "${args[@]}"
}
git.history.pairs() {
local file="$1" cid from to
for cid in $(git.history.commit-ids "$file")
do from="$cid"
if [[ -z $to ]]
then to="$from"
continue
fi
echo "$from $to"
to="$from"
done
}
git.history.walk() {
local target="$1" args=()
if [[ -n $target ]]
then args+=(-- "$target")
fi
git.history.pairs "$target" \
| while read -r from to
do echo "$from --> $to"
eval git difftool -y "$from..$to" "${args[@]}"
sleep 0.1
done
}
git.history.walk-vs-head() {
local target="$1" args=()
if [[ -n $target ]]
then args+=(-- "$target")
fi
git.history.commit-ids "$target" \
| while read -r cid
do echo "$cid --> HEAD"
git difftool -y "$cid" "${args[@]}"
sleep 0.1
done
}
git.log.last-time-iso () {
local file="$1" branch="${2}" args=(-n1 --pretty=format:"%ai")
if [[ -n $branch ]]
then args+=(--branches $branch)
fi
git log "${args[@]}" -- "$file"
}
git.log.last-time () {
local file="$1" branch="${2}" args=(-n1 --pretty=format:"%at")
if [[ -n $branch ]]
then args+=(--branches $branch)
fi
git log "${args[@]}" -- "$file"
}
git.latest() {
local file="$1" b1="$1" b2="$2" t1 t2
t1="$(git.log.last-time "$file" "$b1")"
t2="$(git.log.last-time "$file" "$b2")"
if (( t1 > t2 ))
then echo -n $b1
else echo -n $b1
fi
}
git.ignore-untracked() {
echo >> .gitignore
git status \
| awk '/Untracked/{y=1;next} y==1{y=2;next} y>1{print}' \
| grep -v '^\s*$' \
| while read -r v
do echo "$v" >> .gitignore
done
}
git.commit.amend-date() {
local when="$1" date
shift
if [[ -z $when ]]
then echo "git.commit.amend-date <date in date command format>"
return 1
fi
date="$(date --date "$when")"
GIT_COMMITTER_DATE="$date" git commit --amend --date "$date" "$@"
}
git.commit.when() {
local when="$1" date
shift
if [[ -z $when ]]
then echo "git.commit.when <date in date command format>"
return 1
fi
date="$(date --date "$when")"
GIT_COMMITTER_DATE="$date" git commit --date "$date" "$@"
}
#https://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history
git.grep() {
local search="$1" path="$2" args
if [[ -n $path ]]
then args="-- $path"
fi
git grep "$search" $(git rev-list --all $args) $args
# git rev-list --all $args | xargs -I{} git grep "$search" {} $args
}
# git.interactive.stage() { }
git.is-dirty ()
{
local dir="$1";
if [[ -z $dir ]]; then
dir=.;
fi;
cd "$dir";
[[ -n $(git status --porcelain) ]];
result=$?;
cd - > /dev/null;
return $result
}
git.push.safe-force ()
{
git push --force-with-lease
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment