How to use git merge --squash?

2018-12-31 09:21发布

I have a remote Git server, here is the scenario which I want to perform:

  • For each bug/feature I create a different Git branch

  • I keep on committing my code in that Git branch with un-official Git messages

  • In top repository we have to do one commit for one bug with official Git message

So how can I merge my branch to remote branch so that they get just one commit for all my check-ins (I even want to provide commit message for this)?

7条回答
忆尘夕之涩
2楼-- · 2018-12-31 09:30

You want to merge with the squash option. That's if you want to do it one branch at a time.

git merge --squash feature1

If you want to merge all the branches at the same time as single commits, then first rebase interactively and squash each feature then octopus merge:

git checkout feature1
git rebase -i master

Squash into one commit then repeat for the other features.

git checkout master
git merge feature1 feature2 feature3 ...

That last merge is an "octopus merge" because it's merging a lot of branches at once.

Hope this helps

查看更多
永恒的永恒
3楼-- · 2018-12-31 09:33

For Git

Create a new feature

via Terminal/Shell:

git checkout origin/feature/<featurename>
git merge --squash origin/feature/<featurename>

This doesnt commit it, allows you to review it first.

Then commit, and finish feature from this new branch, and delete/ignore the old one (the one you did dev on).

查看更多
永恒的永恒
4楼-- · 2018-12-31 09:37

Merge newFeature branch into master with a custom commit:

git merge --squash newFeature && git commit -m 'Your custom commit message';

If instead, you do

git merge --squash newFeature && git commit

you will get a commit message that will include all the newFeature branch commits, which you can customize.

查看更多
听够珍惜
5楼-- · 2018-12-31 09:45

Say your bug fix branch is called bugfix and you want to merge it into master:

git checkout master
git merge --squash bugfix
git commit

This will take all the commits from the bugfix branch, squash them into 1 commit, and merge it with your master branch.


Explanation:

git checkout master

Switches to your master branch.

git merge --squash bugfix

Takes all the commits from the bugfix branch and merges it with your current branch.

git commit

Creates a single commit from the merged changes.

Omitting the -m parameter lets you modify a draft commit message containing every message from your squashed commits before finalizing your commit.

查看更多
裙下三千臣
6楼-- · 2018-12-31 09:45

Suppose you worked in feature/task1 with multiple comments

1. Go to your project branch ( project/my_project )
   git checkout project/my_project
2. Create new branch ( feature/task1_bugfix )
   git checkout -b feature/task1_bugfix
3. Marge with ( --squash ) command
   git merge --squash feature/task1
4. Create single commit
   git commit -am "add single comments"
5. Push your branch
   git push --set-upstream origin feature/task1_bugfix
查看更多
冷夜・残月
7楼-- · 2018-12-31 09:46

What finally cleared this up for me was a comment showing that:

git checkout main
git merge --squash feature

is the equivalent of doing:

git checkout feature
git diff main > feature.patch
git checkout main
patch -p1 < feature.patch
git add .

When I want to merge a feature branch with 105(!!) commits and have them all squashed into one, I don't want to git rebase -i origin/master because I need to separately resolve merge conflicts for each of the intermediate commits (or at least the ones which git can't figure out itself). Using git merge --squash gets me the result I want, of a single commit for merging an entire feature branch. And, I only need to do at most one manual conflict resolution.

查看更多
登录 后发表回答