1

Of the few times I have squashed Git commits, it took some manual work to pick the previous commits I wanted to squash.

Is there some way to so this automatically - my goal is to squash all previous commits that have not already been pushed to a particular remote branch.

To elaborate, say I have 1 local branch called "dev" and 2 remotes, public and private. I commit and push all I want to the private remote, to a branch called "dev", let's call that private/dev. But on the public remote I want to keep things clean and tidy, and to keep one standard branch called "master", let's call that public/master. So like I said, for all the commits that have not already made it to the public remote master branch, I want to squash those into one big commit, and the push to public/master

How can I achieve that? Seems complicated.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

3

You simply create a temporary branch via public/master (with public being the name of your public remote, and master - for instance - being the destination branch)

And you use merge --squash (see "In git, what is the difference between merge --squash and rebase?")

 git checkout -b temp public/master
 git merge  --squash dev
 # since merge --squash does not produce a commit:
 git commit -m "squash dev"
 git push public temp:master
 git checkout dev
 git branch -d temp

On the colon syntax, see "git push branch to a new repo with a different name" and the examples section of git push.
It allows for pushing a local branch to a remote one with a different name.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I hope this works. can you please elaborate on what exactly is happening in each step/command? not sure I actually follow – Alexander Mills Oct 22 '16 at 21:48
  • for example, can you give the merge result a commit message, which would be used when you looked at the latest version of public/master? – Alexander Mills Oct 23 '16 at 00:45
  • also what is "git push public tmp:master" - what does the colon syntax mean? – Alexander Mills Oct 23 '16 at 00:46
  • I updated my question with useful details, I will edit your answer to reflect the details, please correct me if I am mistaken, thanks. – Alexander Mills Oct 23 '16 at 00:51
  • I got this: "$ git merge --squash -m "squashed with dev" dev" => Squash commit -- not updating HEAD Automatic merge went well; stopped before committing as requested not sure what that means – Alexander Mills Oct 23 '16 at 05:01
  • I didn't request "not to commit", but I guess it is giving you a chance to commit afterwards and provide a message – Alexander Mills Oct 23 '16 at 05:02
  • 1
    @AlexanderMills Right! `git merge --squash` does not produce a commit. You need an additional `git commit -m`. Edited. – VonC Oct 23 '16 at 05:13
  • but do you know what this means: "squash commit -- not updating HEAD" - it seems to not update the index (does not capture files from dev branch for some reason). Even though it says the automatic merge was succesful. – Alexander Mills Oct 23 '16 at 05:35
  • @AlexanderMills It means it records the merge in the index, but does not commit. files from dev should be there. – VonC Oct 23 '16 at 05:57
  • yeah I am not sure, I got it working but a slightly different way, thanks a lot for your help! – Alexander Mills Oct 23 '16 at 06:19
  • @AlexanderMills Excellent: What did you use? – VonC Oct 23 '16 at 06:25
  • I had to use a 3rd branch to avoid merge conflicts because public/master has deleted files that I remove from public, public/master cannot be merged directly with dev, I have to use a 3rd branch called devtemp where I delete the same files I deleted in master. – Alexander Mills Oct 23 '16 at 16:47