Squashing Git commits after merging

泄露秘密 提交于 2020-02-24 11:02:28

问题


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:

  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



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!