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)?
- 1Do you want to keep the individual commits on those other branches?poke– poke2011-03-15 08:15:11 +00:00CommentedMar 15, 2011 at 8:15
- 40I typically usegit rebase -i to collapse all my commits into one commit and re-write the commit message. Then I send it upstream.Edward Falk– Edward Falk2013-11-27 19:28:24 +00:00CommentedNov 27, 2013 at 19:28
- 45
git merge --squashdoes it all on the command line in one shot and you just hope it works.git rebase -ibrings up an editor and lets you fine-tune the rebase. It's slower, but you can see what you're doing. Also, there are difference between rebase and merge which are a little too involved to address in a comment.Edward Falk– Edward Falk2015-12-17 18:49:24 +00:00CommentedDec 17, 2015 at 18:49 - 12the problem with all these answers is that you have to be on the master branch locally and the run the merge --squash command... I want to run the merge --squash from the feature branch not the master branch..so that when I am done, I can push the feature branch to the remote and submit a PR, is that possible?Alexander Mills– Alexander Mills2016-11-01 23:34:40 +00:00CommentedNov 1, 2016 at 23:34
- 13@AlexanderMills, I think you just need a second feature branch (cloned from the master branch). Do the
merge --squashfrom the old to the new one, and then merge the new branch to master. The old branch becomes obsolete.Sam H– Sam H2018-03-28 16:12:04 +00:00CommentedMar 28, 2018 at 16:12
15 Answers15
Say your bug fix branch is calledbugfix and you want to merge it intomaster:
git checkout mastergit merge --squash bugfixgit commitThis will take all the commits from thebugfix branch, squash them into 1 commit, and merge it with yourmaster branch.
Explanation:
git checkout masterSwitches to yourmaster branch.
git merge --squash bugfixTakes all commits from thebugfix 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 commitCreates 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.
17 Comments
git commit (without-m param) and you will get to modify a drafted commit message containing all commit messages that you squashed.git commit --amend -m '...' later on.git commit will no longer show the useful commit message containing all commit messages you squashed. In that case, trygit commit --file .git/SQUASH_MSG (viastackoverflow.com/a/11230783/923560 ).git commit -a --author="Author" --message="Issue title #id"git merge --squash allows you to create a single commit on top of the current branch whose effect is the same as merging another branch. But it won't produce the merge record, which means your pull-request as result would have no changes, yet won't be marked as merged! So, you will need just to delete that branch to be done.What finally cleared this up for me was acomment showing that:
git checkout maingit merge --squash featureis the equivalent of doing:
git checkout featuregit diff main > feature.patchgit checkout mainpatch -p1 < feature.patchgit add .When I want to merge a feature branch with 105(!!) commits and have them all squashed into one, I don't want togit rebase -i origin/master because I need to separately resolve merge conflicts foreach of the intermediate commits (or at least the ones which git can't figure out itself). Usinggit 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.
8 Comments
git merge master, and only thengit merge --squash feature in the master branch.git merge --squash feature from the master branch?You want to merge with the squash option. That's if you want to do it one branch at a time.
git merge --squash feature1If 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 feature1git rebase -i masterSquash into one commit then repeat for the other features.
git checkout mastergit merge feature1 feature2 feature3 ...That last merge is an "octopus merge" because it's merging a lot of branches at once.
8 Comments
Suppose you worked in feature/task1 with multiple commits.
Go to your project branch (project/my_project)
git checkout project/my_projectCreate a new branch (feature/task1_bugfix)
git checkout -b feature/task1_bugfixMerge with the
--squashoptiongit merge --squash feature/task1Create a single commit
git commit -am "add single comments"Push your branch
git push --set-upstream origin feature/task1_bugfix
Comments
MergenewFeature branch intomaster 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 thenewFeature branch commits, which you can customize.
I explain it thoroughly here:https://youtu.be/FQNAIacelT4
Comments
If you have alreadygit merge bugfix onmain, you can squash your merge commit into one with:
git reset --soft HEAD^1git commit3 Comments
git reset --soft HEAD^1 seems to undo the last commit performedbefore the merge, at least in case of the merge being a fast-forward.git reset --soft HEAD^<number-of-commits-to-squash>.I know this question isn't about Github specifically, but since Github is so widely used and this is the answer I was looking for, I'll share it here.
Github has the ability to perform squash merges, depending on the merge options enabled for the repository.
If squash merges are enabled, the "Squash and merge" option should appear in the dropdown under the "Merge" button.
1 Comment
To squash your local branch before pushing it:
checkout the branch in question to work on if it is not already checked out.
Find the sha of the oldest commit you wish to keep.
Create/checkout a new branch (tmp1) from that commit.
git checkout -b tmp1 <sha1-of-commit>Merge the original branch into the new one squashing.
git merge --squash <original branch>Commit the changes which have been created by the merge, with a summary commit message.
git commit -m <msg>Checkout the original branch you want to squash.
git checkout <branch>Reset to the original commit sha you wish to keep.
git reset --soft <sha1>Rebase this branch based on the new tmp1 branch.
git rebase tmp1That's it - now delete the temporary tmp1 branch once you're sure everything is ok.
Comments
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).
2 Comments
git checkout YOUR_RELEASE_BRANCHgit pullgit checkout -b A_NEW_BRANCHgit merge --squash YOUR_BRANCH_WITH_MULTIPLE_COMMITSgit commit -am "squashing all commits into one"git push --set-upstream origin A_NEW_BRANCH1 Comment
pull. All the other responses seem to assume that nothing has changed in the remote release branch since the last time you were hanging out on it....if you get error: Committing is not possible because you have unmerged files.
git checkout mastergit merge --squash bugfixgit add .git commit -m "Message"fixed all the Conflict files
git add .you could also use
git add [filename]Comments
Your feature branch is done and ready to commit to master, develop or other target branch with only one commit
- Go to merge branch : git checkout master && git pull
- Create a work branch from your clean local master : git checkout -b work
- Merge squash your feature branch on work : git merge --squash your_feature_branch.
- Commit with default or a new message : git commit (with a specific or default message)
- Go back to your feature branch : git checkout your_feature_branch
- Point your feature branch to work dir : git reset --hard work
- Verify but you are ready to push : git push -f
- Then clean up work branch if needed
Replace master with your target branch : develop and so on
- No need to specify how many commit from your master to your feature branch. Git takes care*
Comments
Assume the name of the branch where you made multiple commits is called bugfix/123, and you want to squash these commits.
First, create a new branch from develop (or whatever the name of your repo is). Assume the name of the new branch is called bugfix/123_up. Checkout this branch in git bash -
- git fetch
- git checkout bugfix/123_up
- git merge bugfix/123 --squash
- git commit -m "your message"
- git push origin bugfix/123_up
Now this branch will have only one commit with all your changes in it.
Comments
You can use tool I've created to make this process easier:git-squash. For example to squash all commits on feature branch that has been branched from master branch, write:
git squash mastergit push --forceComments
Use
git statusto check what's going on.
Then
git checkout master git merge --squash bugfixgit add (add which files you want or use wildcard command like ".")Then
git commit -m "message"And now last but not the least
git push -u origin masterHereorigin can be other remote you prefer.
Comments
Explore related questions
See similar questions with these tags.











