0

I am trying to squash 2 commits and remove merge part, but have some problems.

I have branch main and featureBranch. In my terminal I do:

git checkout main
git merge --squash featureBranch
git push

But nothing really happens. I am in the project folder, I did pull and fetch so I have all the necessary data. How to do that. (VCS is bitbucket even though this shouldn't be important)

PS. I want to do the squash without using interactive rebase option (it is much more complicated that way, at least in my opinion)

Kratos
  • 681
  • 1
  • 13
  • 30
  • What exactly does "nothing really happens" mean? What did you expect to happen and how did you determine that it didn't happen? – mkrieger1 Jan 20 '23 at 20:12
  • You want to merge branch or squash commits? – Aissaoui Ahmed Jan 20 '23 at 20:28
  • I want to merge my featureBranch into main brach. I want to perform squash on it so instead of 2 commits I did, in git history it has only 1. Also I would like that in git history "merge" part is no visible (it all shoud be squashed) – Kratos Jan 20 '23 at 20:40
  • 1
    Do yourself a favour and start learning to use interactive rebase. You're missing out on so much of git without it. It really is an essential, non-optional part of git. – hlovdal Jan 20 '23 at 23:30
  • @hlovdal ok I guess you have point, everybody are saying it. Tomorrow I will spend time and learn it. Guys thanks for the comments – Kratos Jan 20 '23 at 23:34

1 Answers1

1

If you have already pushed to origin you will have to rewrite history with a force. If you want to squash commits before pushing you can do

git rebase -i HEAD~<num-commits-to-squash-from-head>

In the interactive page keep a "p" for pick on the commit you want to squash into and replace the command the for rest with "s". After saving this git will let you edit the commit message for the squashed commit

Kilbo
  • 333
  • 1
  • 12
  • yes but this is doing squash with interactive rebase isn't it? I don't want to do that. I want to squash it without interactive rebase. It is possible, right – Kratos Jan 20 '23 at 19:49
  • You can do `git rebase featureBranch` this will still leave you with two commits from feature branch but no "merge commit". `git pull --squash featureBranch` will stage the commits from feature branch which you can then commit ontop of main then push – Kilbo Jan 20 '23 at 20:12
  • yes but if I for example had 10 commits, rebase goes one by one and applies it, right. This can take a long time. I program in android (android studio) and always had headaches from rebase bcs of this. With merge it goes much faster and smoother. That is the reason why I wanted to avoid interactive rebase or rebase at all – Kratos Jan 20 '23 at 20:54
  • 1
    Instead of counting commits, use `git rebase -i $(git merge-base main featureBranch)` – amphetamachine Jan 20 '23 at 21:14
  • 1
    Ok I obviously need to do interactive rebase, everybody suggest that. @Kilbo I will accept answer, thank you for the effort – Kratos Jan 20 '23 at 23:36