0 votes
in GitLab by
How do you squash last N commits into a single commit?

1 Answer

0 votes
by

Squashing multiple commits into a single commit will overwrite history, and should be done with caution. However, this is useful when working in feature branches. To squash the last N commits of the current branch, run the following command (with {N} replaced with the number of commits that you want to squash):

git rebase -i HEAD~{N}                

Upon running this command, an editor will open with a list of these N commit messages, one per line. Each of these lines will begin with the word “pick”. Replacing “pick” with “squash” or “s” will tell Git to combine the commit with the commit before it. To combine all N commits into one, set every commit in the list to be squash except the first one. Upon exiting the editor, and if no conflict arises, git rebase will allow you to create a new commit message for the new combined commit.

Related questions

+1 vote
asked Jul 26, 2019 in Git Slack Integration by sheetalkhandelwal
+1 vote
asked Jul 25, 2019 in Git by SakshiSharma
...