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 com
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 tmp1
That's it - now delete the temporary tmp1 branch once you're sure everything is ok.
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
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.
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.
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