Best way to collapse a bunch of git commits into one?

969 views
Skip to first unread message

Greg Spencer

unread,
Apr 15, 2011, 5:09:05 PM4/15/11
to Chromium OS dev
Say I have a local branch with a bunch of things committed to it (lots of checkpoints, for instance), and I want to rebase it to trunk without having to merge each change separately, because I know it touches the same file the whole way through, and I'll have to merge it 10 times instead of just that final merge.

What's the best way to collapse all those changes into one with git?  I have been doing the following, and it just seems clunky:

1) Open up gitk, select trunk and my top revision and make a patch of the diff between the two revisions (yes, I could probably do this on the command line somehow, but this way I don't have to look up hashes)
2) switch to trunk
3) create a new branch
4) git apply the patch.
5) rebase the new branch to trunk and merge that one commit
6) delete the old branch and rename the new branch with the old name.

I'm sure there's got to be some simple way to do this in git, but it doesn't appear so from the help that I've looked at.

-Greg.

Steven Bennetts

unread,
Apr 15, 2011, 5:17:20 PM4/15/11
to Greg Spencer, Chromium OS dev
git checkout trunk
git checkout -b new_branch
git merge --squash old_branch


--
Chromium OS Developers mailing list: chromiu...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-os-dev?hl=en

Vince Laviano

unread,
Apr 15, 2011, 5:17:14 PM4/15/11
to Greg Spencer, Chromium OS dev
On Fri, Apr 15, 2011 at 2:09 PM, Greg Spencer <gspe...@chromium.org> wrote:

Check out the "squashing a commit" section in particular.

Zdenek Behan

unread,
Apr 15, 2011, 5:17:52 PM4/15/11
to Greg Spencer, Chromium OS dev
http://cheat.errtheblog.com/s/git/

Squashing
---------
WARNING: "git rebase" changes history. Be careful. Google it.

git rebase --interactive HEAD~10
  (then change all but the first "pick" to "squash")
  squash the last 10 commits into one big commit

--

Greg Spencer

unread,
Apr 15, 2011, 5:25:52 PM4/15/11
to Zdenek Behan, Chromium OS dev
Thanks, that helps a lot!

-Greg.

Zdenek Behan

unread,
Apr 17, 2011, 6:49:07 AM4/17/11
to Dominic Battre, Greg Spencer, Chromium OS dev
That works, but interactive rebase preserves all of your log messages and gives you the option to compose a new one made of the old ones. There's many many ways to juggle with patches and recommit.

On Sat, Apr 16, 2011 at 9:15 AM, Dominic Battre <bat...@google.com> wrote:
Just for the record.

I would do:

git checkout my_branch
git reset --soft trunk
git commit -a

It does not require the creation of a second branch nor the manual process of an interactive rebase.

git reset --soft keeps your working copy unchanged but makes my_branch point to the same revision as trunk. In other words, it looks like you had just created my_branch from trunk and did all the changes that are contained in trunk...my_branch).

Best regards,
Dominic

Mandeep Singh Baines

unread,
Apr 21, 2011, 2:09:47 PM4/21/11
to Greg Spencer, Chromium OS dev

I agree with zbehan. git rebase -i is the way to go. But I'd like to highlight
an alternative workflow (what I use). I use git commit --amend when amending
a change I'm working on. I still get the checkpoint functionality via
git reflog.

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages