41

I'm new to git (and enjoying it a lot!). While developing in a new branch, I kept committing the various development 'states' of my application. Now I have to check it in for review but didn't want everything to go in different commits (different comments and ids).

How can I do a push of all changes as if it was the first time?

2
  • @Alex it's fine to close this one (4 years after?) but mine's date is a month older, slightly unfair :)
    – DiogoNeves
    Commented Feb 22, 2015 at 20:35
  • @Alex sounds good to me :)
    – DiogoNeves
    Commented Mar 5, 2015 at 15:53

2 Answers 2

50
git rebase -i HEAD~5

allows you to interactively select which of the 5 last commits to join into one; off the top of my head it opens the editor with something like this

pick xxxx commit1
pick xxxx commit2
pick xxxx commit3
pick xxxx commit4
pick xxxx commit5

you change this into

pick xxxx commit1
squash xxxx commit2
squash xxxx commit3
squash xxxx commit4
pick xxxx commit5

which results in two commits being left: first one that has combined commits 1 - 4, and commit 5 (the newest one) which is left alone

3
  • 4
    You have the order backwards — the commits are listed oldest-first.
    – Josh Lee
    Commented Feb 8, 2011 at 20:06
  • @jleedev +1 you're right, fixed it
    – stijn
    Commented Feb 8, 2011 at 20:45
  • Thanks! :) I actually soft reset to the commit just before the first commit I made and then did a brand new commit of all changes together but I wasn't happy with my solution. Thanks for this one :)
    – DiogoNeves
    Commented Feb 10, 2011 at 11:00
5

I think it's a good idea to keep your "micro commits". You can do a diff from the last commit before your feature to the current HEAD to see the entire diff which you can send for review.

4
  • Its more of a scenario-specific thing; usually you will want to rebase and make commits more logical and get rid of "oops!" commits before sending in patches. Commented Feb 8, 2011 at 22:41
  • 1
    That is true but I don't like squashing the entire week/month of development into a single commit just for the purpose of review. I'm a heavy user of --amend myself. Commented Feb 9, 2011 at 5:34
  • You're probably both right, as mathepic said, needs to be scenario-specific. In my case I'm sending it for others to review and the 'back-up' commits don't make much sense in there :) Thanks
    – DiogoNeves
    Commented Feb 10, 2011 at 10:58
  • 4
    Yes. But you don't have to send them a single commit to review. You can send them a range of commits git diff start end and ask them to review the entire thing. No? Commented Feb 10, 2011 at 18:27

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