Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I merge multiple commits onto another branch as a single squashed commit?

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)?

like image 313
SunnyShah Avatar asked Mar 15 '11 07:03

SunnyShah


People also ask

How do you squash multiple commits in the same branch?

Git's squash commits command There is no explicit Git squash command. Instead, to squash git commits, the interactive git rebase is used. To squash all commits on a single branch, the interactive git rebase command must be passed one of two arguments: the id of the commit from which the branch split from its parent.

Can a merge commit be squashed?

Squashing can be chosen as an option while merging in Git by adding --squash to your merge command. Squashing can also be overridden with --no-squash .

How do I merge squash and branch commits?

You can choose to squash merge when completing a pull request in Azure Repos. Choose Squash commit under Merge type in the Complete pull request dialog to squash merge the topic branch.


1 Answers

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 commits from the bugfix branch and groups it for a 1 commit with your current branch.
(no merge commit appears; you could resolve conflicts manually before following commit)

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.

like image 160
abyx Avatar answered Sep 19 '22 13:09

abyx