Created
September 16, 2011 16:24
-
-
Save nikreiman/1222474 to your computer and use it in GitHub Desktop.
Git safe pull
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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