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.