2

Whenever I do a git pull on the server, I get the message;

Your branch is ahead of origin/master by 2 commits.

The two should be in sync. I don't know how I got here. Everything I have in origin/master I want on the server. I don't care if I lose the changes for the last couple of commits, it was nothing vital. How do I get these back in sync so I can push and pull like normal again?

Edit: I ran git checkout origin/master and got my code on the server back to a previous state, but now I'm in detached HEAD state. How do I get out of that?

Jo Sprague
  • 16,523
  • 10
  • 42
  • 62
  • Check out http://stackoverflow.com/questions/1741143/git-pull-origin-mybranch-leaves-local-mybranch-n-commits-ahead-of-origin-why – Thilo Apr 01 '13 at 14:45
  • @DanMoulding hah.. I should have checked for that. – AD7six Apr 01 '13 at 14:59

2 Answers2

1

This message means that the master branch on your server, has commits that are not in the remote. If someone committed on the server and you aren't expecting anyone to ever commit from this particular checkout - that would cause this situation.

To see what the two commits are

.. that are not in the remote:

git log origin/master..HEAD # just the commit messages
git diff origin/master..HEAD # the diff

To remove these two commits

If the two commits that exist you don't want to keep; to change your master branch to exactly match the master branch of the remote:

git checkout master # make sure you are on your master branch
git reset origin/master --hard # force local history to exactly match the remote.

To keep these two commits

If you do want to keep the commits - simply push them to the remote and your master branch will then be in sync:

git push origin master
AD7six
  • 63,116
  • 12
  • 91
  • 123
1

AD7six's answer will discard your local changes. You should also be able to simply send what you have locally to the server:

git checkout master # Get back on a real branch from the detached HEAD
git push origin master

Also, gitk --all is very useful for understanding the repository state you have and how your local repository think it relates to its remotes.

Phil Miller
  • 36,389
  • 13
  • 67
  • 90
  • `Whenever I do a git pull on the server` <- `gitk` is only going to work if it's installed and executed locally (i.e. it won't work executed on a remote server via ssh). It also [doesn't show anything you can't see directly with git anyway](http://stackoverflow.com/a/5361546/761202). – AD7six Apr 01 '13 at 14:58