Skip to content

Instantly share code, notes, and snippets.

@trustin
Created April 27, 2011 11:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trustin/944104 to your computer and use it in GitHub Desktop.
Save trustin/944104 to your computer and use it in GitHub Desktop.
Some useful git scripts I wrote - use with 'git config --global alias.<name> '!<script path>'
#!/bin/bash
# Git-aware bash command prompt
#
# Put this script at $HOME/.bash_prompt and add the following to your .bashrc:
#
# if [ "$PS1" ]; then
# if [ -f "$HOME/.bash_prompt" ]; then
# PROMPT_COMMAND="$HOME/.bash_prompt"
# if [ "$EUID" == "0" ]; then
# PS1="\n# "
# else
# PS1="\n$ "
# fi
# fi
# fi
# Terminal title
printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
# Current user, host name, and working directory
echo -n "$USER@${HOSTNAME%%.*}:${PWD/#$HOME/~}"
# Git information
GIT_STATUS="$(git status 2> /dev/null)"
if [[ "$?" == "0" ]]; then
echo
# Origin
GIT_REMOTE="$(git remote --verbose)"
if [[ ${GIT_REMOTE} =~ (origin[^a-z]*([^ ]*) *\(push\)) ]]; then
GIT_ORIGIN="${BASH_REMATCH[2]}"
if [[ ${GIT_ORIGIN} =~ (git@github\.com:([-_a-zA-Z0-9/\.]*)\.git) ]]; then
GIT_ORIGIN="${BASH_REMATCH[2]}"
fi
echo -ne "[git:\033[1;34m${GIT_ORIGIN}\033[0m]"
unset GIT_ORIGIN
else
echo -n "[git]"
fi
unset GIT_REMOTE
# Highlight with a color
echo -ne "\033[1;32m"
# Various status
if [[ ${GIT_STATUS} =~ (# On branch ([-_a-zA-Z0-9\.]*)) ]]; then
echo -n " ${BASH_REMATCH[2]}"
fi
if [[ ${GIT_STATUS} =~ (working directory clean) ]]; then
echo -n " clean"
else
echo -n " dirty"
fi
if [[ ${GIT_STATUS} =~ (# Your branch is ([a-z]*) of ([-_\'a-zA-Z0-9/\.]*) by ([0-9]*) commit) ]]; then
echo -n " ${BASH_REMATCH[2]}:${BASH_REMATCH[4]}"
fi
if [[ ${GIT_STATUS} =~ (# Your branch and ([^ ]*) have diverged) ]]; then
echo -n " diverged"
fi
echo -ne "\033[0m"
fi
unset GIT_STATUS
#!/bin/bash
if test $# -ne 1; then
echo "Usage: $0 <file>"
exit 1
fi
git log --reverse "$1" | head -3 | tail -1 | grep -oE '[12][0-9][0-9][0-9]'
#!/bin/bash
(
# Run in the subshell because the script calls 'exit' instead of 'return'.
. /usr/share/doc/git-[1-9].*/contrib/workdir/git-new-workdir "$@"
)
if [ "$?" != '0' ]; then
exit 1
fi
#!/bin/bash
if test $# -lt 1 || test $# -gt 2; then
echo "Usage: $0 <remote branch name> [<remote>]"
exit 1
fi
if [ "x$2" == "x" ]; then
git push origin ":$1"
else
git push "$2" ":$1"
fi
#!/bin/bash
function echo_and_run() {
echo -ne "\033[1;32m"
echo -n "$@"
echo -e "\033[0m"
"$@"
}
BRANCH=$(git symbolic-ref HEAD)
RESULT=$?
if [[ "$RESULT" -ne 0 ]]; then
exit $RESULT
fi
if [[ "$BRANCH" =~ ([^/]+$) ]]; then
BRANCH=${BASH_REMATCH[1]}
else
echo Cannot find the current branch.
exit 128
fi
# Pull from origin
if git branch -r | grep -qE "^\\s*origin/$BRANCH($|\s)"; then
echo_and_run git pull --ff-only origin "$BRANCH"
RESULT=$?
if [[ $RESULT -ne 0 ]]; then
exit $RESULT
fi
else
echo "Remote branch 'origin/$BRANCH' does not exist."
echo "Run 'git push origin "$BRANCH"' to push to the origin."
exit 128
fi
# Check if the current workdir is clean with no additional commit
STATUS=$(git status)
PUSH_TO_ORIGIN=false
if [[ "$STATUS" =~ (working directory clean) &&
! "$STATUS" =~ (# Your branch is ([a-z]*) of [^ ]+ by ([0-9]*) commit) ]]; then
PUSH_TO_ORIGIN=true
fi
# Pull from upstream (if available)
if git remote | grep -qE '^upstream$'; then
echo_and_run git pull --ff-only upstream "$BRANCH"
RESULT=$?
if [[ $RESULT -ne 0 ]]; then
exit $RESULT
fi
# Push the commits from upstream if necessary
if $PUSH_TO_ORIGIN; then
STATUS=$(git status)
if [[ "$STATUS" =~ (working directory clean) &&
"$STATUS" =~ (# Your branch is ahead of [^ ]+ by ([0-9]*) commit) ]]; then
echo_and_run git push origin "$BRANCH"
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment