8

I want to squash two latest commits with commit messages "first" and "second". First I pull master then I use the command

git rebase -i HEAD~2 master 

It shows me both commits in an editor like this:

pick first pick second 

Then I change this editor as:

pick first squash second 

After saving the changes I got this message:

Successfully rebased and updated refs/heads/master. 

It did change anything in remote master. To apply these changes I use the git push command and got 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 (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

Then I again run the git pull command and it merge origin master and make another commit with this commit message:

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

After this when I run git push, it squash the two commits into one with message "first". The problem is that in remote master, I have now four commits:

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

Where I want only one commit which is the squashed one with commit message"first". Any ideas where I am doing mistake??

1
  • You don't have to squash anymore: the owner can do it for you (since March 2016): see stackoverflow.com/a/36377439/6309 Commented Apr 2, 2016 at 19:01

1 Answer 1

10

git rebase rewrites history, because the commits were modified. It's not a problem when the said commits haven't been pushed to a remote repository, but here the remote was previously pushed with the commits you rewrote, so it rejected the push.

When you pulled from github, it merged both histories, and applied your squash commit, hence the mess.

Conclusion: when you want to rewrite commits that has already been pushed, you have two options:

  • don't do it
  • do a git push --force, which will rewrite history also on the remote, and tell people that you have rewrote history, so they'll see strange things on their next pull.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.