I want to crush several commits on github

I want to squash the last two commits with the commit messages “first” and “second”. First I pull out the master, then I use the command

git rebase -i HEAD~2 master 

It shows that both commits in the editor are as follows:

 pick first pick second 

Then I change this editor as follows:

 pick first squash second 

After saving the changes, I received this message:

 Successfully rebased and updated refs/heads/master. 

This has not changed anything remotely. To apply these changes, I use the git push command and received the following error:

 To https://github.com/aneelatest/GITtest.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/test/GITtest.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (eg 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

Then I run the git pull command again and it combines the original master and makes another commit with this commit message:

 Merge branch 'master' of https://github.com/aneelatest/GITtest 

After that, when I run git push, it goes through two commits into one with the message “first”. The problem is that the remote master now has four commits:

 first second Merge branch 'master' of https://github.com/test/GITtest first 

Where I want only one commit, which is crushed with the message "first". Any ideas where I'm wrong?

+7
github
source share
1 answer

git rebase overwrites history because commits have been changed. This is not a problem when the specified commits were not transferred to the remote repository, but here the remote one was previously pressed using the commits that you rewrote, so it rejected push.

When you pulled from github, he combined both stories and applied your squash commit, hence the mess.

Conclusion: if you want to rewrite the commits that have already been pressed, you have two options:

  • do not do this.
  • do a git push --force which will rewrite the story also on the remote control and let people know that you rewrote the story so that they see strange things the next time you press.
+9
source share

All Articles