Skip to content

Instantly share code, notes, and snippets.

@AhiyaHiya
Last active March 31, 2020 14:54
Show Gist options
  • Save AhiyaHiya/905ce97f1e92d5ae5353966bab732d7d to your computer and use it in GitHub Desktop.
Save AhiyaHiya/905ce97f1e92d5ae5353966bab732d7d to your computer and use it in GitHub Desktop.
git functions to make life easier
#!/usr/bin/env bash
# 2019-2020 AhiyaHiya
# git functions to make life easier
# Edit shell helper
alias eshg="code $HOME/Development/Tools/Scripts/shell_helpers_git.sh"
#####################
# aliases first; if it requires a description, then it is just made into a function.
alias gta="git add"
alias gtc="git commit -m"
alias gtf="git fetch --prune"
alias gtpl="git pull"
alias gtps="git push"
alias gts="git status"
#####################
# git functions
function gt_get_filepath_from_filename() {
local FILENAME=$1
local FILEPATH=$1
if [ ! -f "${FILEPATH}" ]; then
FILEPATH=$(qfind "${FILENAME}")
fi
echo "${FILEPATH}"
}
# abort merge
function gtam() {
git reset --hard HEAD
}
# clean for current branch
function gtcc() {
local -r CURRENT_BRANCH=$(gtlcbc)
git fetch
git checkout -f "${CURRENT_BRANCH}"
git reset --hard origin/"${CURRENT_BRANCH}"
git reset --hard
git clean -x -d -f
SUBMODULE_STATUS=$(git submodule status)
if [[ "$SUBMODULE_STATUS" != "" ]]; then
git submodule foreach git reset --hard
fi
}
# git create new branch
function gtcnb() {
git checkout -b "$1"
}
# git checkout branch
function gtcob() {
local -r BRANCH=$1
git checkout "$BRANCH"
}
# git checkout file
function gtcof() {
local -r FILEPATH=$(gt_get_filepath_from_filename "$1")
if [ ! -f "${FILEPATH}" ]; then
echo "File $FILENAME not found"
return
fi
git checkout "${FILEPATH}"
}
# git launch diff file helper, comparing file to state in current branch
function gtdf() {
local -r FILEPATH=$(gt_get_filepath_from_filename "$1")
if [ ! -f "${FILEPATH}" ]; then
echo "File $FILENAME not found"
return
fi
git difftool --no-prompt "${FILEPATH}"
}
# git launch diff file helper comparing file in current-branch against specified branch
function gtdfb() {
# Usage: gtdfb SOURCE_BRANCH FULL_OR_PARTIAL_FILEPATH
local -r SOURCE_BRANCH=$1
local -r FILEPATH=$(gt_get_filepath_from_filename "$2")
echo "$FILEPATH"
if [ ! -f "${FILEPATH}" ]; then
echo "File $FILENAME not found"
return
fi
local -r CURRENT_BRANCH=$(gtlcbc)
git difftool --no-prompt "$SOURCE_BRANCH" "$CURRENT_BRANCH" -- "${FILEPATH}"
}
# git launch diff file helper comparing file in current-branch against master
function gtdfm() {
# Usage: gtdfm FULL_OR_PARTIAL_FILEPATH
local -r FILEPATH=$(gt_get_filepath_from_filename "$1")
echo "$FILEPATH"
if [ ! -f "${FILEPATH}" ]; then
echo "File $FILENAME not found"
return
fi
local -r CURRENT_BRANCH=$(gtlcbc)
git difftool --no-prompt master "$CURRENT_BRANCH" -- "${FILEPATH}"
}
# git launch difftool, comparing all of the changed files in current branch against files in master
function gtdiff_branch() {
local -r CURRENT_BRANCH=$(gtlcbc)
git diff --name-only ${CURRENT_BRANCH} "$(git merge-base ${CURRENT_BRANCH} master)" 2>/dev/null | while read -r FILEPATH; do
git difftool --no-prompt master "$CURRENT_BRANCH" -- "${FILEPATH}" &
done
}
# diff file to previous commit
function gtdfp() {
git difftool HEAD@{1}
}
# git edit branch description
function gtebd() {
git branch --edit-description
}
# git empty commit ; just follow this with a message
function gtec() {
git commit --allow-empty -m
}
# list all branches
function gtlab() {
git --no-pager branch -a
}
# git list current branch
function gtlcb() {
git branch --no-color 2>/dev/null | sed -e '/^[^*]/d' -e "s/* \\(.*\\)/\\1$(parse_git_dirty)/"
}
# git list current branch CLEAN
function gtlcbc() {
echo $(gtlcb) | tr -d '*'
}
# for graph at command line
function gtlg1() {
# e.g. gtlg1 --all
# e.g. gtlg1 --nomerges
git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)'
}
# for graph at command line
function gtlg2() {
# e.g. gtlg1 --nomerges
git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)'
}
# git reset submodule recursively
function gtrsr() {
git submodule foreach --recursive git reset --hard
# get a fresh copy
git submodule update --init
}
# git list changed files by author
function gtlcfba() {
# for a date range, use --after="2018-05-01" --until="2018-05-14"
local authorname=$1
local after=$2
local until=$3
git log --pretty="%H" --author="${authorname}" --no-merges --after="${after}" --until="${until}" |
while read -r commit_hash; do
git show --oneline --name-only "$commit_hash" | tail -n+2
done | sort | uniq
}
# git list local branch(es)
# nice function thanks to https://github.com/bahmutov/git-branches#adding-to-bash_profile
function gtllb() {
branch=""
branches=$(git branch --list)
while read -r branch; do
clean_branch_name=${branch//\*\ /}
description=$(git config branch."$clean_branch_name".description)
printf "%-15s %s\\n" "$branch" "$description"
done <<<"$branches"
}
# Nice functions from https://gist.github.com/dciccale/5560837
function parse_git_dirty() {
git diff --quiet --ignore-submodules HEAD 2>/dev/null
[ $? -eq 1 ] && echo "*"
}
# git print branch description
function gtpbd() {
local -r CURRENT_BRANCH=$(gtlcbc)
git config branch."${CURRENT_BRANCH}".description
}
# git list changed files in current-branch against parent
function gtlcf() {
# will default to master if parent branch not passed in
local -r PARENT_BRANCH=${1:-master} # defaults to master branch
local -r CURRENT_BRANCH=$(gtlcbc)
git diff --name-only ${CURRENT_BRANCH} "$(git merge-base ${CURRENT_BRANCH} ${PARENT_BRANCH})" 2>/dev/null | while read -r CHANGES; do
echo "$CHANGES"
done
}
# git past day
function gtpd() {
# commits in the past day
git --no-pager log --no-merges --since="1 day ago"
}
# git stash file
function gtsf() {
# Usage gtsf FILEPATH "Message for stash, not optional"
# This is also in Alfred as a workflow shortcut
local -r FILEPATH=$(gt_get_filepath_from_filename "$1")
local -r MESSAGE="$2"
git stash push -m "$MESSAGE" "$FILEPATH"
}
# git remove files from current commit
function gt_remove_files_from_current_commit()
{
git reset HEAD^
}
# git revert changed files
function gt_revert_files()
{
# This is essentially the same as git checkout each changed file.
git reset --hard
}
# Allows you to search for a string inside the code, across all revisions
function gt_search_string_in_commited_code()
{
local searchString=${1:?}
git grep "$searchString" $(git rev-list --all)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment