soham
soham

Reputation: 1676

How do I squash all commit history and push into another remote repository?

I have not been able to figure out after much trying. I have two local branches, master and tests. I have two corresponding remote branches with the same repo, origin/master, origin/tests. I have another remote branch public/master. I have some pushed in commit history on both local master and remote origin/master. Now, I want to squash all the commits of origin/master and push into the remote branch public/master. I can't figure out how to do it.

I have tried doing rebase on a new local branch but it didn't work.

Upvotes: 3

Views: 2036

Answers (2)

Shaun Luttin
Shaun Luttin

Reputation: 141512

Reset to your first commit, then amend, finally force push.

git pull origin master
git checkout master
git reset --soft <my-first-commit>
git commit --amend -m "New commit message"
git push public master --force-with-lease

If the last command gives a "stale info" error, either

  • run git fetch public master before the last command, or
  • run git push --force-with-lease public +master instead of the last command.

You can find your first commit like this:

git rev-list --max-parents=0 HEAD

Upvotes: 7

eftshift0
eftshift0

Reputation: 30212

Could also work by checking out an orphan branch (which will carry the current working tree to make the very first commit), and then you can push into whatever remote you feel like.

Upvotes: 1

Related Questions