Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push to remote after a git rebase?

Tags:

git

github

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

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

git push

It gives:

To [email protected]:my_user/my_repo.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]: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
like image 802
tread Avatar asked Mar 26 '14 07:03

tread


2 Answers

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
like image 89
amnn Avatar answered Sep 22 '22 09:09

amnn


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.

like image 32
SzG Avatar answered Sep 26 '22 09:09

SzG