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.

2

1 Answer 1

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
1
  • 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. Commented Nov 17, 2020 at 2:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.