Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Squash commits directly on feature without rebase or merge

I've been reading a little about --squashing commits, but they all seem to be go hand in hand with a --rebase.

I have a feature branch with a set of commits like this:

(Feature)          A --> B --> C --> D --> E --> F --> G
                  /
(Master)  M1 --> M2 --> M3

Suppose I want to merge back to the Master branch, but I want to clean up the commits on my feature first.

Is it possible to:

  • Pick commit B, E and F and squash them together as one commit?

OR

  • Can I only squash the commits that come in order, so squash: (A, B and C), or squash (D, E and F) etc?

Either way, can I do a squash directly on my feature, WITHOUT immidiately initializing a Merge or Rebase with it?

If so, how can I do this with Git?

like image 431
Vivendi Avatar asked Jul 14 '15 14:07

Vivendi


People also ask

Can you squash commits without merging?

Squash commits directly on feature without rebase or merge.

How do I squash multiple commits without using git merge?

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.


1 Answers

In my team's workflow we often merge with upstream in the middle of a bunch of commits, and rebasing could get ugly. I've found this helpful to squash all commits that are ahead of master down into one:

# Commit any working changes on branch "mybranchname", then...
git checkout master
git checkout -b mybranchname_temp
git merge --squash mybranchname
git commit -am "Message describing all squashed commits"
git branch -m mybranchname mybranchname_unsquashed
git branch -m mybranchname

# Optional cleanup:
git branch -D mybranchname_unsquashed

# If squashing already-pushed commits...
git push -f
like image 141
BoltzmannBrain Avatar answered Oct 21 '22 11:10

BoltzmannBrain