Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to squash all commits from a commit id?

I have a lot of commits on my dev branch and I want to squash them into a single commit before creating a merge request. I want to squash all commits from id: 9199ed7e53dbeeecbb9ef107e245f4d07b2ff257 including the last commit: 6177ddbf5b8e681a028abd7c512ae6ccdc86e744

I think i need to use the git rebase interactive but I don’t want to do some manual work for 50+ commits :/ any suggestions?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In your case,

# move head to the last commit
git reset 6177ddbf5b8e681a028abd7c512ae6ccdc86e744 --hard

# move head to 9199ed7e53dbeeecbb9ef107e245f4d07b2ff257 and keep all changes in the index
git reset 9199ed7e53dbeeecbb9ef107e245f4d07b2ff257 --soft

# amend the changes into 9199ed7e53dbeeecbb9ef107e245f4d07b2ff257
git commit --amend

Here we can use a slightly different method,

# move head to the last commit
git reset 6177ddbf5b8e681a028abd7c512ae6ccdc86e744 --hard

# move head to 9199ed7e53dbeeecbb9ef107e245f4d07b2ff257's parent and keep all changes in the index
git reset 9199ed7e53dbeeecbb9ef107e245f4d07b2ff257~ --soft

# commit the changes
git commit

Interactive rebase can also work but it needs a bit more editing.

# specify 9199ed7e53dbeeecbb9ef107e245f4d07b2ff257's parent as the base
git rebase -i 9199ed7e53dbeeecbb9ef107e245f4d07b2ff257~

# modify the "pick" from the 2nd line to the last line to "s" or "squash"
# with the help of editor like vim or vs code, it's easy to edit multiple lines
# save and quit

# edit the commit message
# save and quit
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading