Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining multiple commits before pushing in Git [duplicate]

Tags:

git

git-squash

I have a bunch of commits on my local repository which are thematically similar. I'd like to combine them into a single commit before pushing up to a remote. How do I do it? I think rebase does this, but I can't make sense of the docs.

like image 498
muudscope Avatar asked Aug 04 '11 00:08

muudscope


People also ask

Can we do multiple commits before pushing?

Committing takes place only within your repository; it has nothing to do with whether or not you're online. The things that you need to be online for are pushing (publishing your commits to another repository) and pulling (fetching and merging commits from another repository).


2 Answers

What you want to do is referred to as "squashing" in git. There are lots of options when you're doing this (too many?) but if you just want to merge all of your unpushed commits into a single commit, do this:

git rebase -i origin/master 

This will bring up your text editor (-i is for "interactive") with a file that looks like this:

pick 16b5fcc Code in, tests not passing pick c964dea Getting closer pick 06cf8ee Something changed pick 396b4a3 Tests pass pick 9be7fdb Better comments pick 7dba9cb All done 

Change all the pick to squash (or s) except the first one:

pick 16b5fcc Code in, tests not passing squash c964dea Getting closer squash 06cf8ee Something changed squash 396b4a3 Tests pass squash 9be7fdb Better comments squash 7dba9cb All done 

Save your file and exit your editor. Then another text editor will open to let you combine the commit messages from all of the commits into one big commit message.

Voila! Googling "git squashing" will give you explanations of all the other options available.

like image 121
Leopd Avatar answered Sep 18 '22 11:09

Leopd


If you have lots of commits and you only want to squash the last X commits, find the commit ID of the commit from which you want to start squashing and do

git rebase -i <that_commit_id> 

Then proceed as described in leopd's answer, changing all the picks to squashes except the first one.

Example:

871adf OK, feature Z is fully implemented      --- newer commit --┐ 0c3317 Whoops, not yet...                                         | 87871a I'm ready!                                                 | 643d0e Code cleanup                                               |-- Join these into one afb581 Fix this and that                                          | 4e9baa Cool implementation                                        | d94e78 Prepare the workbench for feature Z     -------------------┘ 6394dc Feature Y                               --- older commit 

You can either do this (write the number of commits):

git rebase --interactive HEAD~[7] 

Or this (write the hash of the last commit you don't want to squash):

git rebase --interactive 6394dc 
like image 34
Noich Avatar answered Sep 17 '22 11:09

Noich