Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git squash all commits in branch without conflicting

Tags:

git

rebase

squash

A common development workflow for us is to checkout branch b, commit a bunch to it, then squash all those commits into one (still on b).

However, during the rebase -i process to squash all the commits, there are frequently conflicts at multiple steps.

I essentially want to alter the branch into one commit that represents the state of the repository at the time of the final commit on b

I've done some searching but I haven't found exactly what I'm looking for. I don't want to merge --squash because we would like to test the squashed feature branch before merging.

like image 983
Andy Ray Avatar asked Jun 27 '13 22:06

Andy Ray


People also ask

Can I squash commits without rebasing?

Solution: It is possible to squash multiple commits into single one without rebasing.

How could you squash multiple commits together without using merge -- squash?

You can do this fairly easily without git rebase or git merge --squash . In this example, we'll squash the last 3 commits. Both of those methods squash the last three commits into a single new commit in the same way. The soft reset just re-points HEAD to the last commit that you do not want to squash.

How do you squash all the commits?

Squashing by Interactive Rebase. Git's interactive rebase will list all relevant commits in the default editor. In this case, those are the commits we want to squash. Then we can control each commit and commit message as we want and save the change in the editor.

How could you squash multiple commits without?

To "squash" in Git means to combine multiple commits into one. You can do this at any point in time (by using Git's "Interactive Rebase" feature), though it is most often done when merging branches. Please note that there is no such thing as a stand-alone git squash command.


3 Answers

If you don't need the commit information, then you could just do a soft reset. Then files remain as they were and when you commit, this commit will be on top of the commit you did reset to.

To find the commit to reset to:

git merge-base HEAD BRANCH_YOU_BRANCHED_FROM

Then

git reset --soft COMMIT_HASH

Then re-craft the commit, perhaps:

git commit -am 'This is the new re-created one commit'
like image 143
Rasmus Østergaard Kjær Voss Avatar answered Oct 23 '22 22:10

Rasmus Østergaard Kjær Voss


This is simlar to the answer from Rasmus but broken down into three steps that should always work:

$ git merge feature1
$ git reset --soft HEAD@{1}
$ git commit -c feature1

Explanation:

  1. merge and resolve conflicts
  2. keep changes staged but reset to old head
  3. commit all changes using commit message and author from feature branch latest commit
like image 20
ColinM Avatar answered Oct 23 '22 22:10

ColinM


I've created a tool specifically for this task:

https://github.com/sheerun/git-squash

For example you can call following to squash all commits from "master" commit up to HEAD, no matter how many conflicts there would be along the way:

git squash master

It's basically what GitHub does when you "squash and merge" pull request.

like image 2
sheerun Avatar answered Oct 23 '22 22:10

sheerun