Skip to content

Instantly share code, notes, and snippets.

@jvns
Last active November 12, 2023 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jvns/0f45c910ea2d255c6e130299c99c3123 to your computer and use it in GitHub Desktop.
Save jvns/0f45c910ea2d255c6e130299c99c3123 to your computer and use it in GitHub Desktop.
# very VERY crappy version of `git rebase` that assumes that there are no merge conflicts.
# the goal is to try to show the relationship between "rebase" and "merge" by
# showing how you can implement `git rebase` using repeated `git merge`s into a copy of the target branch
set -euo pipefail
TARGET=$1
CURRENT=$(git rev-parse --abbrev-ref HEAD)
# Make a copy of the target branch to work in
git checkout -b temp-rebase-branch $TARGET
# Cherry pick commits onto the target branch, one at a time
for COMMIT in $(git rev-list --reverse --topo-order $TARGET..$CURRENT); do
git merge --no-commit --no-ff $COMMIT
git reset HEAD
git commit -a -C $COMMIT
done
# Force the branch we're rebasing to point at our temp rebase branch
git branch -f $CURRENT temp-rebase-branch
# Go back to the current branch
git checkout $CURRENT
# Delete the temporary branch
git branch -d temp-rebase-branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment