Squash my last X commits together using Git

0 votes

How can I squash my last X commits together into one commit using Git?

Feb 16 in Others by Soham
• 8,730 points
6 views

1 answer to this question.

0 votes

You can do this relatively easily without using a git rebase or git merge --squash. In this example, we'll squash the last 3 commits.

If you want to write the new commit message from scratch, this suffices:

git reset --soft HEAD~3 && 
git commit

If you want to start editing the new commit message with a concatenation of the existing commit messages (i.e. similar to what a pick/squash/squash/…/squash git rebase -i instruction list would start you with), then you need to extract those messages and pass them to git commit:
 

git reset --soft HEAD~3 && 
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

Both of those methods squash the last three commits into a single new commit in the same way. The soft reset just re-points HEAD to the last commit that you do not want to squash. Neither the index nor the working tree are touched by the soft reset, leaving the index in the desired state for your new commit

answered Feb 16 by Aditya
• 7,280 points

Related Questions In Others

0 votes
1 answer

git revert last commit

To answer your question, you can try ...READ MORE

answered Feb 8 in Others by Soham
• 8,730 points
30 views
0 votes
1 answer

Get last modified object from S3 using AWS CLI

With aws s3 ls $BUCKET —recursive, you ...READ MORE

answered Mar 24 in Others by Edureka
• 8,820 points
67 views
0 votes
1 answer
0 votes
1 answer

Using/Handling colon inside a JSF generated HTML element ID in CSS selector

Yes, you can.  Just Backslash (\) the colon.  Like ...READ MORE

answered Nov 14, 2018 in Others by DataKing99
• 8,240 points
1,609 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
1,583 views
+2 votes
1 answer
0 votes
1 answer

Windows git "warning: LF will be replaced by CRLF", is that warning tail backward?

 Depending on the editor you are using, ...READ MORE

answered Feb 16 in Others by Aditya
• 7,280 points
12 views
0 votes
1 answer

How to style a checkbox using CSS

In modern browsers which include Internet Explorer ...READ MORE

answered Feb 17 in Others by Aditya
• 7,280 points
29 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP