575

I am getting the following when running git status

Your branch is ahead of 'origin/master' by 3 commits.

I have read on some other post the way to fix this is run git pull --rebase but what exactly is rebase, will I lose data or is this simple way to sync with master?

8
  • 28
    I don't think this is a duplicate... This question is asking what does it mean, while the other question is asking how to discard the changes.
    – onionjake
    Feb 5, 2014 at 20:44
  • 17
    How was this marked as a duplicate by so many people? The question obviously indicates the person doesn't want to loose their changes. They've made changes and are confused by the message. The so-called duplicate question would have the person loose their changes. Apr 1, 2014 at 23:13
  • 5
    I have to say that the question linked to above is not really a duplicate of this question...
    – Raydot
    Mar 2, 2015 at 20:53
  • 20
    @DerekGreer: how was this marked as a duplicate? Because most people who mark questions as duplicates don't bother to actually read and understand the question. If there are superficial similarities, they will jump to the conclusion that the two are identical, and it will be up to the OP or others who are willing to take the time to carefully repeat what should have been obvious in the first place, if the dupe-hunters had actually cared to pay attention.
    – iconoclast
    Jun 16, 2016 at 19:08
  • 8
    ^^^ the practice of which is killing SO, IMO. Jan 6, 2017 at 5:34

11 Answers 11

1223

You get that message because you made changes in your local master and you didn't push them to remote. You have several ways to "solve" it and it normally depends on how your workflow looks like:

  • In a good workflow your remote copy of master should be the good one while your local copy of master is just a copy of the one in remote. Using this workflow you'll never get this message again.
  • If you work in another way and your local changes should be pushed then just git push origin assuming origin is your remote
  • If your local changes are bad then just remove them or reset your local master to the state on remote git reset --hard origin/main
15
  • 149
    git reset --hard origin/master is exactly what I was looking for. Thanks.
    – FluxEngine
    Apr 29, 2013 at 21:23
  • 4
    @iberbeu you solved my day...git reset --hard origin/master is what I am looking for. +1ed
    – Ravi
    Jul 17, 2013 at 14:30
  • 100
    Also fwiw git diff master origin/master (ie. git diff local remote) to see changes you'll be removing
    – Shanimal
    Jul 10, 2014 at 3:17
  • 1
    I have local/master on the remote origin/branch so using git push origin master:branch which returned Everything up-to-date, after that the message of being ahead by x commits went away.
    – Will B.
    Nov 11, 2014 at 16:28
  • 3
    Last one got me what I needed there!
    – RyanG
    Nov 19, 2015 at 20:19
64

Use these 4 simple commands

Step 1 : git checkout <branch_name>

This is obvious to go into that branch.

Step 2 : git pull -s recursive -X theirs

Take remote branch changes and replace with their changes if conflict arise. Here if you do git status you will get something like this your branch is ahead of 'origin/master' by 3 commits.

Step 3 : git reset --hard origin/<branch_name>

Step 4 : git fetch

Hard reset your branch.

Enjoy.

48

There is nothing to fix. You simply have made 3 commits and haven't moved them to the remote branch yet. There are several options, depending on what you want to do:

  • git push: move your changes to the remote (this might get rejected if there are already other changes on the remote)
  • do nothing and keep coding, sync another day
  • git pull: get the changes (if any) from the remote and merge them into your changes
  • git pull --rebase: as above, but try to redo your commits on top of the remote changes

You are in a classical situation (although usually you wouldn't commit a lot on master in most workflows). Here is what I would normally do: Review my changes. Maybe do a git rebase --interactive to do some cosmetics on them, drop the ones that suck, reorder them to make them more logical. Now move them to the remote with git push. If this gets rejected because my local branch is not up to date: git pull --rebase to redo my work on top of the most recent changes and git push again.

7
  • I used git pull --rebase, but it now says I am ahead by one commit
    – FluxEngine
    Apr 29, 2013 at 21:12
  • 1
    So I made the changes, pushed to master, and then our team lead merged to master. So the changes are there I just need to get synced with the current master.
    – FluxEngine
    Apr 29, 2013 at 21:18
  • @MartyMcFly It's hard to see what is going on here. You say you already pushed? So why do you still commits that aren't on master? What do you mean by your team lead merged to master? Didn't you say you pushed to master already? What does the extra commit contain? Try git diff origin/master to see how your local branch differs from the remote.
    – pmr
    Apr 29, 2013 at 21:21
  • 1
    thanks for the help, sorry if I did a bad job explaining the situation. But what I was looking for is git reset --hard origin/master. But your answer was helpful +1.
    – FluxEngine
    Apr 29, 2013 at 21:25
  • I seem to be stuck between a rock and a hard place, I tried to commit changes, then git said: you are ahead 11 commits. I am not allowed to update the remote repository, so a push is not possible. "git pull" says: Already up-to-date. So I tried your third suggestion, but then git says: cannot pull with rebase: You have unstaged changes. please commit or stash them. Duh, that's how I got here in the first place :-( Nov 29, 2016 at 10:26
29

Came across this issue after I merged a pull request on Bitbucket.

Had to do

git fetch

and that was it.

2
  • 3
    I was working with a repo from a bundle and was able to get rid of the message by applying "git fetch" to a current bundle. tnx! Sep 11, 2017 at 12:17
  • 2
    On a read-only checkout my git status showed I was 2 commits ahead, the log looked as it should - all commits in the origin. I did a git pull and was 5 commits ahead..WTF???? Just needed to fetch to refresh local indexes...all good :) Jun 1, 2020 at 22:02
23

If your git says you are commit ahead then just First,

git push origin

To make sure u have pushed all ur latest work in repo

Then,

git reset --hard origin/master

To reset and match up with the repo

1
  • git push origin worked for me. Thanks!
    – pawan
    Feb 3 at 4:15
18

Usually if I have to check which are the commits that differ from the master I do:

git rebase -i origin/master

In this way I can see the commits and decide to drop it or pick...

1
  • 2
    This lowly answer all the way down here was what I needed to do. I couldn't figure out how to find out the difference, and all my various git diff ... magicks wouldn't work. When I did this, it gave me noop as the only commit, and when I accepted it, now my branch is in-sync with origin/master. So it appears the commits diff from origin/master were in effect nothing. Feb 24, 2020 at 16:23
11

This message from git means that you have made three commits in your local repo, and have not published them to the master repository. The command to run for that is git push {local branch name} {remote branch name}.

The command git pull (and git pull --rebase) are for the other situation when there are commit on the remote repo that you don't have in your local repo. The --rebase option means that git will move your local commit aside, synchronise with the remote repo, and then try to apply your three commit from the new state. It may fail if there is conflict, but then you'll be prompted to resolve them. You can also abort the rebase if you don't know how to resolve the conflicts by using git rebase --abort and you'll get back to the state before running git pull --rebase.

8

This happened to me once after I merged a pull request on Bitbucket.

I just had to do:

git fetch

My problem was solved. I hope this helps!!!

1
6

git reset --hard origin/master

Use this command

1

I had this problem and I used 'git reset --hard origin/master' without quotes to rebase my local master to the remote master branch.

1

As others said, you have local changes that you committed but didn't push.

If you're unsure about which changes are there, and you want to check before doing a push, the command below will tell you which files changed:

git diff --stat origin/master..

And if you remove --stat, it will show the diff of all files.

Not the answer you're looking for? Browse other questions tagged or ask your own question.