Consider your new feature branch is called bugfix
. You have done a lot of
commits eg. 20 commits to the branch and want to squash all commits to one before merging to
main
.
Standard squash
The standard way is like this.
git checkout bugfix
git rebase -i HEAD~20
Alternative 1 (tried)
You only need to use git add .
if there are new files other than the already tracked files.
git checkout bugfix
git reset --soft master
git add .
git commit -m "Commit message"
git push --force
Alternative 2 (tried)
This method unfortunately doesn’t work when running in Emacs. You need to use the terminal. If the commits e.g 4 commits, have been pushed, then you can merge these commits by doing:
git checkout main
git reset --soft HEAD~4
git git commit -m "Your new commit message"
git push origin +main
Use of plus sign eg. +main
is to ensure to force push to only one branch.
Using --force
might overwrite multiple push destinations configured with
remote.*.push
other than the current branch.
Alternative 3
git rebase -i master
Then you have to specify what to squash and to pick. Here is the example if
bugfix
have only 4 commits.
pick fda59df commit 1
squash x536897 commit 2
squash c01a668 commit 3
squash c01a668 commit 4
Then merge to main
as normal procedure
git checkout main
git merge --no-ff bugfix
For detail can read here
Alternative 4
git checkout main
git pull
git merge --squash bugfix
git commit --amend -m "Commit message to ammend to"
git push --set-upstream origin main
#or
git push origin +main
You can also read from others eg. Gist