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.

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

I hope this works. can you please elaborate on what exactly is happening in each step/command? not sure I actually follow
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?
also what is "git push public tmp:master" - what does the colon syntax mean?
I updated my question with useful details, I will edit your answer to reflect the details, please correct me if I am mistaken, thanks.
@AlexanderMills Right! git merge --squash does not produce a commit. You need an additional git commit -m. Edited.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.