2

I realized I had set my user.name up wrong, so I used this answer to fix the incorrect commits. Locally, it worked great. However, git now tells me

$ git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 15 and 15 different commits each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
nothing to commit, working directory clean

So, now I need to somehow send my changes to my GitHub repository, but when I push:

$ git push
WARNING: gnome-keyring:: couldn't connect to: /run/user/mspencer/keyring-uscL41/pkcs11: No such file or directory
Username for 'https://github.com': iBeliever
Password for 'https://iBeliever@github.com': 
To https://github.com/iBeliever/weather-desktop.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/iBeliever/weather-desktop.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.

I'm not quite sure how to fix this. I've dealt with remote changes before, and solved them by pulling and merging, but I'm not sure what to do in this case. I need to update my GitHub repository with the corrected names, how do I do that?

Community
  • 1
  • 1
iBelieve
  • 1,502
  • 17
  • 32

2 Answers2

5

Steven's answer is correct but very dangerous. So be careful when you use a forced push.

First of all, git push -f will force push all local branches to the remote repository.

To just push the master branch, you must specify it.

git push -f origin master

When doing this you will want to tell all the other people that have access to this repository to pull down your changes before they make any additional commits. Otherwise the branches will diverge in crazy ways that you don't want to deal with.

atreat
  • 4,243
  • 1
  • 30
  • 34
  • I've read several times that `git push -f` is dangerous, but I don't know why. Is it only dangerous when other people have clones of the repository? I am the only one working on the project, so is it safe in this case? – iBelieve Apr 18 '13 at 19:00
  • It is dangerous because you are forcing an update to the remote repository that could lose commits. If you had the git log A->B->C it is possible to locally checkout commit B, then force push an update to the remote repository. The remote would then have the history A->B. If you are the only one who has access to the repository and you are pushing a state of the repository that you are comfortable with. Then you are should be free and clear. Just know that git push -f origin can have non-reversible consequences. – atreat Apr 18 '13 at 19:06
1

You can force the push

git push -f

More info

      -f, --force
          Usually, the command refuses to update a remote ref that is not an
          ancestor of the local ref used to overwrite it. This flag disables
          the check. This can cause the remote repository to lose commits;
          use it with care.
Zombo
  • 1
  • 62
  • 391
  • 407