问题
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?
回答1:
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:
- Ensure your working tree is clean. No uncommitted changes, no staged changes, no untracked files.
- Ensure you are on the feature branch.
git reset --hard *Hash of D on feature branch*
git reset --soft *Hash of A on master*
git commit
with the message you desire.git merge master
回答2:
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.
来源:https://stackoverflow.com/questions/33512202/squashing-git-commits-after-merging