Skip to content

Instantly share code, notes, and snippets.

@aramalipoor
Last active November 25, 2017 17:12
Show Gist options
  • Save aramalipoor/fa0a501d985b095b4d8ec453e1ba8e56 to your computer and use it in GitHub Desktop.
Save aramalipoor/fa0a501d985b095b4d8ec453e1ba8e56 to your computer and use it in GitHub Desktop.
Re-commit snippet to commit your last commit again with same message to your remote, without adding a new commit to history

Install

sudo curl -L -o /usr/local/bin/recommit https://gist.githubusercontent.com/aramalipoor/fa0a501d985b095b4d8ec453e1ba8e56/raw/032275f887799c139a6b06e148b1146c914313b9/recommit.sh
sudo chmod +x /usr/local/bin/recommit

Disclaimer

Although this snippet checks last commit's ID on your remote branch before tying to force commit, use it at your own risk.

#!/usr/bin/env bash
REMOTE=${1:-origin}
REMOTE_URL=$(git remote show ${REMOTE} | egrep "Push\\s*URL" | cut -d' ' -f6)
BRANCH=$(git symbolic-ref --short -q HEAD)
LAST_MESSAGE=$(git log -1 HEAD --pretty=format:%s)
LAST_LOCAL_COMMIT_ID=$(git log --format="%H" -n 1)
LAST_REMOTE_COMMIT_ID=$(git ls-remote ${REMOTE_URL} refs/heads/${BRANCH} | awk '{print $1}')
echo "-------------------------------------------------------------"
echo "- REMOTE=$REMOTE"
echo "- REMOTE_URL=$REMOTE_URL"
echo "- BRANCH=$BRANCH"
echo "- LAST_LOCAL_COMMIT_ID=$LAST_LOCAL_COMMIT_ID"
echo "- LAST_REMOTE_COMMIT_ID=$LAST_REMOTE_COMMIT_ID"
echo "- LAST_MESSAGE=$LAST_MESSAGE"
echo "-------------------------------------------------------------"
if [[ ! ${LAST_LOCAL_COMMIT_ID} == ${LAST_REMOTE_COMMIT_ID} ]]; then
echo "Last local commit ID is not eual to last remote commit ID"
exit 1
fi
git reset --soft HEAD~1
git add . --all
git commit -m "$LAST_MESSAGE"
git push $REMOTE $BRANCH --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment