Skip to content

Instantly share code, notes, and snippets.

@jackkoenig
Last active October 5, 2023 20:09
Show Gist options
  • Save jackkoenig/06f2605af4b4fb138af5dc1d06c77fa2 to your computer and use it in GitHub Desktop.
Save jackkoenig/06f2605af4b4fb138af5dc1d06c77fa2 to your computer and use it in GitHub Desktop.

Sometimes you want to do the equivalent of a Github Squash-and-Merge without the merge.

I originally had the stuff below the line, but it turns out you can just do this with git merge --squash, see https://gist.github.com/aortbals/2aeb557bf127dd7ae88ea63da93479fc.

git checkout <my branch>
git checkout -b <my branch>-backup
git checkout <base branch>
git pull
# Make sure you have created <my branch>-backup !!!
git branch -D  <my branch>
git merge --squash <my branch>-backup
git checkout -b <my branch>
git commit

If you want a pre-populated message similar to what you get in a Github squash and merge, the following is helpful:

git log --reverse --pretty=format:"%B" <my branch> --not <base branch> > log.txt

log.txt will contain something sort of like what Github generates (although lacking * and coauthors).


EDIT: This is a manual way of doing it, but you should just do what's above the line

Here's a way of doing it with git locally assuming you have merge the base branch back into your branch such that the only diff is the changes on your branch.

git diff origin/<base branch> > patch.diff
git log --reverse --pretty=format:"%B" <my branch> --not <base branch> > log.txt
git reset --hard origin/<base branch>
git apply patch.diff
git add -A :^log.txt :^patch.diff
git commit -F log.txt

This may be a built-in feature for such functionality: https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt---rebase-mergesrebase-cousinsno-rebase-cousins

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