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, 2022 in Others by Soham
• 9,710 points
488 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, 2022 by Aditya
• 7,680 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, 2022 in Others by Soham
• 9,710 points
714 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, 2022 in Others by gaurav
• 23,260 points
4,757 views
0 votes
1 answer

Want Tick mark and X-mark using current date and current time using Excel formula

I likewise use a checkmark to indicate ...READ MORE

answered Nov 10, 2022 in Others by narikkadan
• 63,640 points
905 views
0 votes
1 answer

How to find the last row in a column using openpyxl normal workbook?

ws.max_row will give you the number of rows ...READ MORE

answered Dec 25, 2022 in Others by narikkadan
• 63,640 points
6,520 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
4,011 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, 2022 in Others by Aditya
• 7,680 points
5,782 views
0 votes
1 answer

How to style a checkbox using CSS

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

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