0

I'm on the master branch and in hindsight, I have pushed too many commit messages (or at least my teammates tell me so). Here's what the log looks like:

% git log --oneline
fc71bc8 (HEAD -> mainline, origin/master, origin/HEAD) Renamed 'week' to 'period'
3858ca5 Reverted days in year back to 365
a5d19bb Reverted days in year back to 365
635a4fe Submitting revision 2
edb25c7 Removed var_int as it is no longer needed
af1c576 Added unit tests and addressed comments
72b95a1 Built infrastructure for modeling
d31b45a added augmented model

I want to squash commits 3858ca5, a5d19bb, 635a4fe, edb25c7, and af1c576. That would leave only commits fc71bc8, 72b95a1, and d31b45a. Of course, I want to retain the code changes in the squashed commits, I just want them to be wrapped into commit 72b95a1. How can I best do this?

I want to reiterate that these commits have already been pushed to master on the remote repository. I'm not solely talking about commits on my local computer as was done here.

Gaurav Bansal
  • 5,221
  • 14
  • 45
  • 91
  • https://stackoverflow.com/search?q=%5Bgit%5D+squash+multiple+commits – phd Nov 16 '20 at 23:07
  • 2
    More appropriate duplicate: [How to squash commits in git after they have been pushed?](https://stackoverflow.com/q/5667884/2745495) – Gino Mempin Nov 17 '20 at 06:03

1 Answers1

3

That is normally done with rebase -i:

git rebase -i d31b45a
# first revision will be 72b95a1, with pick, leave it like that
# the following IDs for the revisions you want to squash, set them to squash
# leave the one for fc71bc8 in pick
# save and exit
# rebase will start running and will open on the editor so you set the message
# for all those squashed commits
# set the message, save  and exit and rebase should finish
# and the squashed revisions should be there
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • This would work for code that hasn't already been pushed. What if I need to do this for code that is already pushed to the master? Would this override previous pushes? I modified my question to emphasize the fact that the code is already pushed. – Gaurav Bansal Nov 17 '20 at 02:34