[git] How to use git merge --squash?

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

This question is related to git git-merge git-squash

The answer is


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.


Suppose you worked in feature/task1 with multiple commits.

  1. Go to your project branch (project/my_project)

    git checkout project/my_project
    
  2. Create a new branch (feature/task1_bugfix)

    git checkout -b feature/task1_bugfix
    
  3. Marge with the --squash option

    git merge --squash feature/task1
    
  4. Create a single commit

    git commit -am "add single comments"
    
  5. Push your branch

    git push --set-upstream origin feature/task1_bugfix
    

If you have already git merge bugfix on main, you can squash your merge commit into one with:

git reset --soft HEAD^1
git commit

Use

git status 

to check what's going on.

Then

git checkout master 
git merge --squash bugfix
git 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 master

Here origin can be other remote you prefer.


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 master
git push --force

if you get error: Committing is not possible because you have unmerged files.

git checkout master
git merge --squash bugfix
git add .
git commit -m "Message"

fixed all the Conflict files

git add . 

you could also use

git add [filename]

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.

Screenshot of "Squash and merge" Github feature


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*

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.

I explain it thoroughly here: https://youtu.be/FQNAIacelT4


git checkout YOUR_RELEASE_BRANCH
git pull
git checkout -b A_NEW_BRANCH
git merge --squash YOUR_BRANCH_WITH_MULTIPLE_COMMITS
git commit -am "squashing all commits into one"
git push --set-upstream origin A_NEW_BRANCH

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.


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


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


To squash your local branch before pushing it:

  1. checkout the branch in question to work on if it is not already checked out.

  2. Find the sha of the oldest commit you wish to keep.

  3. Create/checkout a new branch (tmp1) from that commit.

    git checkout -b tmp1 <sha1-of-commit>

  4. Merge the original branch into the new one squashing.

    git merge --squash <original branch>

  5. Commit the changes which have been created by the merge, with a summary commit message.

    git commit -m <msg>

  6. Checkout the original branch you want to squash.

    git checkout <branch>

  7. Reset to the original commit sha you wish to keep.

    git reset --soft <sha1>

  8. Rebase this branch based on the new tmp1 branch.

    git rebase tmp1

  9. That's it - now delete the temporary tmp1 branch once you're sure everything is ok.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to git-merge

Abort a Git Merge Git pull - Please move or remove them before you can merge Git: How configure KDiff3 as merge tool and diff tool Git: How to pull a single file from a server repository in Git? How to resolve git error: "Updates were rejected because the tip of your current branch is behind" error: Your local changes to the following files would be overwritten by checkout Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch How to merge specific files from Git branches git remove merge commit from history The following untracked working tree files would be overwritten by merge, but I don't care

Examples related to git-squash

Combining multiple commits before pushing in Git How to use git merge --squash? Squash my last X commits together using Git