Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

want to squash multiple commits in github

Tags:

github

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??

like image 617
user2460869 Avatar asked Jul 16 '13 07:07

user2460869


1 Answers

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.
like image 134
CharlesB Avatar answered Sep 23 '22 08:09

CharlesB