Skip to content

Instantly share code, notes, and snippets.

@nikreiman
Created September 16, 2011 16:24
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 nikreiman/1222474 to your computer and use it in GitHub Desktop.
Save nikreiman/1222474 to your computer and use it in GitHub Desktop.
Git safe pull
function git-pull-safe() {
local currentBranch=$(git-branch-current)
local localLastCommit=$(git log --format="%H" $currentBranch | head -1)
local localLastPushCommit="$(git log --format="%H" origin/${currentBranch}.. | tail -n-1)^"
#local remoteLastCommit=$(git log origin/$currentBranch | head -1 | cut -f 2 -d ' ')
git fetch origin $currentBranch
local remoteHeadCommit=$(git log --format="%H" origin/$currentBranch | head -1)
if [ "$remoteHeadCommit" = "$localLastCommit" ] ; then
# Same message as git pull prints in this case
printf "Already up-to-date.\n"
return
fi
git --no-pager log --format="$gitLogFormatOneline" origin/$currentBranch ${localLastPushCommit}..HEAD \
--not $(git --no-pager log --format="%H" $currentBranch ${localLastPushCommit}..HEAD)
local reply=
echo
while read -p "What should we do now? (merge/diff/quit) " reply ; do
if [ "$reply" = "m" -o "$reply" = "merge" ] ; then
git merge origin/$currentBranch
break
elif [ "$reply" = "d" -o "$reply" = "diff" ] ; then
git diff origin/$currentBranch ${localLastPushCommit}..HEAD
elif [ "$reply" = "q" -o "$reply" = "quit" ] ; then
return
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment