Skip to content

Instantly share code, notes, and snippets.

@kyamaguchi
Last active December 14, 2015 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyamaguchi/548d8b03880251f6827a to your computer and use it in GitHub Desktop.
Save kyamaguchi/548d8b03880251f6827a to your computer and use it in GitHub Desktop.
git fixup
#!/bin/bash
# https://github.com/deiwin/git-dotfiles/blob/docs/bin/git-fixup
set -e
# Get the reference to the commit before creating the fixup commit,
# because, say, if the argument is relative to the HEAD then the
# commit it refers to will change once we create a new commit on the
# HEAD.
COMMIT_TO_BE_FIXED=`git show --format=%H --no-patch $1`
git commit --fixup=$COMMIT_TO_BE_FIXED
# Stash unstaged changes (the staged changes will already be in the
# commit created above) to avoid not being able to run the rebase
# command.
if [[ -n $(git status -s | grep -v '??') ]]
then
git stash
HAS_STASHED_CHANGES=true
fi
GIT_SEQUENCE_EDITOR='true' git rebase --interactive --autosquash $COMMIT_TO_BE_FIXED~
if [[ -n "$HAS_STASHED_CHANGES" ]]
then
git stash pop
fi
@kyamaguchi
Copy link
Author

Added grep -v '??' to fix unnecessary stash pop when index and stage are clean and untrack files exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment