Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining various commits in one without merging

Tags:

git

Is it possible to join various commits?

This is my case:

My app runst in OSX 10.6 and 10.7 so I have fixed some stuff for 10.6 then committed, changed to 10.7 and fixed the fixes again so they are compatible, then commit again. then went back to 10.6 and check it again and make a small commit again, etc. (three or four commits in total and all these commits are for the same issue)

As you see all these commits are closely related so I would like to join them, Is this possible?

I still have to work in one more issue so I don't plan to merge the current branch. Ideally I would like to have only one commit per issue/bug I solve.

EDIT:

I have pushed my commits since I have to do this in different computers but the branch is not being used by someone else, not yet.

like image 724
nacho4d Avatar asked Jul 18 '11 03:07

nacho4d


2 Answers

A nice, interactive way is with git rebase -i. Check out a branch, look at the history, and pick a commit that's before the first commit you want to "join" (it's called squashing). Then

git rebase -i <the commit>

In an editor, you'll be shown a list of commits from the one right after the one you chose to the most recent one. It looks like

pick 2f4b7fa Some commit message
pick 19f58bd Some other commit message

Find the first commit in the ones you want to join. Leave that one set to "pick". Then, for all the ones you want to squash into that, change the "pick" to "squash" or just "s". Marking a commit "squash" means that it will be combined with the commit immediately before (above) it. Then save and exit. You'll be prompted for a new commit message for the new commit that will be created. Save that and exit, and you're done. Note that you can also use the rebase view to move commits around by shuffling the lines around. So if you have some commits that are out of order or you need to move commits together to squash them, you can do that, too. Another note: if you've pushed your commits to a remote, this can jack things up, especially if you're working with other people who pull from that remote.

Edit: Since you've pushed the branch already, and you know that nobody else is using it, just follow the above steps, and then do a git push origin master -f, assuming the remote repo is "origin" and you're on the master branch. That's a normal push, but the -f tells it to overwrite whatever is on the remote and force your changes to be applied instead. It's when you're working from a repo shared by others that this becomes dangerous and/or confusing.

like image 130
Ryan Stewart Avatar answered Oct 12 '22 00:10

Ryan Stewart


Have you pushed your commits? If not, you can try git rebase -i. It has a squash option to combine commits. Here's a useful link.

like image 24
Ravi Avatar answered Oct 12 '22 00:10

Ravi