8

Hi I successfully removed a bad commit using: git rebase -p 0df8fg5^ 0df8fg5

I am working on the master branch. However now when I:

git push

It gives:

To git@github.com:my_user/my_repo.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:my_user/my_repo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

git status gives:

$ git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 2 and 3 different commits each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
nothing to commit, working directory clean
tread
  • 10,133
  • 17
  • 95
  • 170
  • 2
    **rule 1: don't rebase/amend commits that you already pushed somewhere.** If you still want to: http://stackoverflow.com/a/3968449/520162 – eckes Mar 26 '14 at 07:26
  • 1
    Alright, then what is the alternative? – tread Mar 26 '14 at 07:27
  • 1
    you'll need to `-f`orce the push as the answers tell you. – eckes Mar 26 '14 at 07:28
  • Clearly, but that doesn't answer the question. You said `don't rebase ...` I'm asking what the alternative would be to remove a specific erroneous commit further back in history – tread Mar 26 '14 at 07:30
  • 1
    If you don't want to force the push, you cannot remove a commit from history once you pushed it somewhere. However, you could `git revert` the single commit that was wrong. This introduces a new commit that reverts the changes of the wrong one but doesn't remove the wrong commit from history. This is safe to be pushed then. – eckes Mar 26 '14 at 07:32
  • Does this answer your question? [Git push rejected after feature branch rebase](https://stackoverflow.com/questions/8939977/git-push-rejected-after-feature-branch-rebase) – Organic Advocate May 28 '20 at 01:56

3 Answers3

5

It's saying that you have diverged from master, because it appears to git as though there is a point where you have commits that origin/master doesn't have and origin/master has commits that you don't have. That point is the point at which you removed the commit from your local copy of master (The rebase changes the sha's of the following commits).

To fix this you have to force push. BUT by default, pushing pushes all your local branches to a branch with the same name in the remote, which means doing a force push could overwrite other people's changes.

You can change the default behaviour of git in this regard with a config setting:

git config --global push.default <setting>

where <setting> can be upstream, current, or simple to prevent pushing to all branches you have local versions of. The default before Git 2 is matching which has the overwriting behaviour I was talking about, whereas the default behaviour from Git 2 onwards is simple. Here's an excerpt from the git man-page describing what each of these settings actually does:

  • current - push the current branch to update a branch with the same name on the receiving end. Works in both central and non-central workflows.

  • upstream - push the current branch back to the branch whose changes are usually integrated into the current branch (which is called @{upstream}). This mode only makes sense if you are pushing to the same repository you would normally pull from (i.e. central workflow).

  • simple - in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch's name is different from the local one.

    When pushing to a remote that is different from the remote you normally pull from, work as current. This is the safest option and is suited for beginners.

    This mode will become the default in Git 2.0.

-- git-config man-page

The command for force pushing is:

git push -f
Community
  • 1
  • 1
amnn
  • 3,657
  • 17
  • 23
2

You have to force the push

git push -f

If others are also using the remote's master branch this will surprise them a great deal. Not necessarily in a positive way.

SzG
  • 12,333
  • 4
  • 28
  • 41
  • What has your team member done before? What is the exact output of `git status` after the push? – SzG Mar 26 '14 at 08:06
2

After a rebase, you need to generally do a force push to remote.

$ git push -f origin master:master

What this will do is forcibly update master without caring about fast-forwarding

Achrome
  • 7,773
  • 14
  • 36
  • 45