user2490003
user2490003

Reputation: 11890

Squashing Git commits after merging

I have a feature branch that has a few commits (see below). Since branching, a few commits have been added to master too.

So I merged master into the feature branch.

git checkout feature
git merge master

This results in the below log history on the feature branch:

*    E (HEAD) merging master into feature
|\
| *  D feature commit 2
| |
| *  C feature commit 1
* |  B master commit 2
|/
*    A master commit 1

In reality the number of commits on the feature path is large (100+) and I'd like to squash them all.

Is there a way to just squash the commits on the feature branch only while preserving those on the master line?

Upvotes: 3

Views: 956

Answers (2)

Xantopp
Xantopp

Reputation: 74

Iff the Question adresses just the case where all commits just live in the local repository, the Question is (apart from my coment) solved. In case only a locale git repository is involved it is aquivalent to:

git checkout feature
git rebase -i
      // mark all commits to be squashed
git merge master

but you should resets masterbranch to E in order to publish your changes:

git checkout master
git reset --hard feature

or (iff you really prefer to work with the comitish) git reset --hard *Hash of E*. I'm going to check weather the use of git reset works if C was already pushed to the remote repository.

Upvotes: 0

Nayuki
Nayuki

Reputation: 18533

Yes you can. So you want to turn the commit graph in your question into this new graph:

*    E (HEAD) merging master into feature
|\
| *  D feature commit (squashes 1 and 2)
* |  B master commit 2
|/
*    A master commit 1

The procedure:

  1. Ensure your working tree is clean. No uncommitted changes, no staged changes, no untracked files.
  2. Ensure you are on the feature branch.
  3. git reset --hard *Hash of D on feature branch*
  4. git reset --soft *Hash of A on master*
  5. git commit with the message you desire.
  6. git merge master

Upvotes: 3

Related Questions