Skip to content

Instantly share code, notes, and snippets.

@Daniel-M
Last active April 11, 2019 17:14
Show Gist options
  • Save Daniel-M/47e3e925740c590a9ce55b24502555d7 to your computer and use it in GitHub Desktop.
Save Daniel-M/47e3e925740c590a9ce55b24502555d7 to your computer and use it in GitHub Desktop.
git_helper bash function
git_helper() {
usage(){
echo "Usage: git_aid [command]"
echo ""
echo "Where [command] is any of,"
echo " * last commit. Shows last commit"
echo " * last branch. Shows last commit"
echo " * recent branches. Shows a list of recent branches and commits with committer"
echo " * recent commits. Shows a list of recent branches and commits with committer"
echo " * diff commits. Shows the difference between the two last commits"
return 1
} # define is_file_exits function
[[ $# -eq 0 ]] && usage
git fetch --all
if [ "${1}" == "last" ]; then
if [ "${2}" == "commit" ]; then
git log --color --format="%C(magenta)%cr %C(bold red)%d%Creset %C(bold cyan)%H%Creset %s %C(bold blue)<%an>%Creset" | head -n 1
elif [ "${2}" == "branch" ]; then
git branch -r | grep -v HEAD | while read b; do git log --color --format="%ci _%C(magenta)%cr %C(bold red)$b%d%Creset%C(bold cyan) %H%Creset %s %C(bold blue)<%an>%Creset" $b | head -n 1; done | sort -r | cut -d_ -f2- | sed 's;origin/;;g' | head -n 1
else
usage
echo ""
echo "Unrecognized command '$1'"
fi
elif [ "${1}" == "recent" ]; then
if [ "${2}" == "branches" ] || [ "${2}" == "commits" ]; then
git branch -r | grep -v HEAD | while read b; do git log --color --format="%ci _%C(magenta)%cr %C(bold red)$b%d%Creset%C(bold cyan) %H%Creset %s %C(bold blue)<%an>%Creset" $b | head -n 10; done | sort -r | cut -d_ -f2- | sed 's;origin/;;g' | head -n 10
fi
elif [ "${1}" == "diff" ]; then
if [ "${2}" == "commits" ]; then
previousToLast=$(git branch -r | grep -v HEAD | while read b; do git log --format="%ci %cr %H" $b | head -n 10; done | sort -r | cut -d_ -f2- | sed 's;origin/;;g' | head -n 2 | tail -n 1 | awk '{print $NF}')
lastCommit=$(git branch -r | grep -v HEAD | while read b; do git log --format="%ci %cr %H" $b | head -n 10; done | sort -r | cut -d_ -f2- | sed 's;origin/;;g' | head -n 1 | awk '{print $NF}')
git diff $previousToLast^..$lastCommit
fi
else
usage
echo ""
echo "Unrecognized command :("
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment