3

I would like to be able to squash the last n commits that have a commit message of 'squash commit' into a single commit from a node.js script. The only way that I know of to squash commits is through an interactive rebase, is there a way to do this from a node script?

Ex:

> git log --oneline | head -4
ecce153 Do not change commit
dd831f6 squash commit
c20677a squash commit
86b52fb squash commit

> ./squash.js "New commit made by squashing 3 'squash commit' commits."
ecce153 Do not change commit
as34dwf New commit made by squashing 3 'squash commit' commits.

Going off of this SO post, https://stackoverflow.com/a/5956198/626759, I could get to the interactive rebase easily enough, but how could I work with that from a node script?

Thanks.

1 Answer 1

5

You don't need to do a rebase to squash n commits:

git reset HEAD~4
git add .
git commit -m "squashed four commits"

This backs up HEAD four commits, but preserves the present changes in the working directory, then adds and commits those changes in a single commit.

Even shorter version

git reset --soft HEAD~4
git commit -m "squashed four commits"

--soft preserves the index too, so you don't have to add.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.