Home > other >  How to squash all commits from a commit id?
How to squash all commits from a commit id?

Time:08-05

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?

CodePudding user response:

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
  •  Tags:  
  • git
  • Related