Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine multiple commits in to one without losing history

I have multiple commits in one branch. When I push these commits for code review, our code review tool creates one review for each commit.

To avoid this I want to merge multiple commits to a single commit. At the same time I don't want to lose the history of commits. Is it possible to create a new branch only for review and combine all the commits in my master branch to a single commit in my review branch? How do I do this?

like image 432
Sirish Avatar asked May 31 '11 09:05

Sirish


2 Answers

I dont know your review tool, but this may work

git checkout review
git merge --squash master

http://www.kernel.org/pub/software/scm/git/docs/git-merge.html

like image 69
KingCrunch Avatar answered Oct 13 '22 01:10

KingCrunch


You do not want to do it. The actual history in the sense of how you did the commit is worthless. The suitably-sized-for-review chunks are interesting, so just rebase -i your working branch to combine the commits to manageably-sized and most importantly logical chunks, post them to review and forget about the original history.

That said, to combine the commits, you can use:

git merge --squash

Git will not throw away the history. It does not actually do that in rebase either, but in that case the previous version of the history only remains accessible through the "reflog" and only if you have expiration for reflogs (IIRC it's 90 days by default), it will eventually become inaccessible and git gc will throw it away. But with the squash, the original branch simply remains there and you can still name the revision from reflog.

However the reviewed history will be merged to the central master by the review tool and nobody will ever see your original history. You can keep it around, but it will not be related to the official one and nobody will ever look at it anyway.

like image 28
Jan Hudec Avatar answered Oct 13 '22 01:10

Jan Hudec