3

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.

soham
  • 1,508
  • 6
  • 30
  • 47

2 Answers2

7

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
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • The last command (`git push public master --force-with-lease`) gives error. `! [rejected] master -> master (stale info)` The public/master also has some content in it. But that should not matter. – soham Jun 20 '17 at 22:22
  • 1
    Okay. It worked. But I needed to do `git pull public master` before pushing. So, please add that command, and I'll accept the answer. Thank you so much! – soham Jun 20 '17 at 22:27
  • 1
    @soham.m17 Sure. I used `fetch` instead of `pull`, though, because we only need to communicate with the remote to update our stale info. We do not actually need to pull. I also gave another option that is likely to work. – Shaun Luttin Jun 20 '17 at 22:33
  • 1
    Okay. For others and future reference, this is what I used: `git pull public master --allow-unrelated-histories` – soham Jun 20 '17 at 22:35
1

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.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • But that will squash all the commit history that I have made in the other remote branch. – soham Jun 20 '17 at 22:24
  • Is that a question or a comment? If it's a question: if you want to replace the content of what is your new branch into a remote branch, just use ```push --force```. If it's a comment: Your remote branch won't be touched until you push something into it. – eftshift0 Jun 20 '17 at 22:31
  • Sorry, I wanted to say, that will *not* squash all the commit history. – soham Jun 20 '17 at 22:34
  • what do you mean? If you do an "orphan" checkout, the branch will have _no_ commits in its history. So if you commit, you will get the "very first" commit on the branch (which is just like a squash... minus the comments). – eftshift0 Jun 20 '17 at 22:40
  • Well, I tried that but it was taking all my commit history from the different branch. May be, I was making some mistake. But I am not sure. The upper commands work pretty well. – soham Jun 20 '17 at 22:42