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?

Machavity
  • 30,841
  • 27
  • 92
  • 100
DiogoNeves
  • 1,727
  • 2
  • 17
  • 36

2 Answers2

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

stijn
  • 34,664
  • 13
  • 111
  • 163
  • 4
    You have the order backwards — the commits are listed oldest-first. – Josh Lee Feb 08 '11 at 20:06
  • @jleedev +1 you're right, fixed it – stijn Feb 08 '11 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 Feb 10 '11 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.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • 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. – alternative Feb 08 '11 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. – Noufal Ibrahim Feb 09 '11 at 05: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 Feb 10 '11 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? – Noufal Ibrahim Feb 10 '11 at 18:27