428

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?

hippietrail
  • 13,703
  • 15
  • 87
  • 133
FluxEngine
  • 10,860
  • 13
  • 49
  • 78
  • 23
    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 05 '14 at 20:44
  • 13
    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. – Derek Greer Apr 01 '14 at 23:13
  • 1
    @DerekGreer as soon as I'm finished getting a dupe hammer for Git, I'll start reopening and properly closing questions like this as duplicates of [other Your branch is ahead of 'origin/master' questions](https://www.google.com/search?q=Your+branch+is+ahead+of+%27origin%2Fmaster%27). –  Jul 21 '14 at 15:08
  • 5
    I have to say that the question linked to above is not really a duplicate of this question... – Dave Kanter Mar 02 '15 at 20:53
  • 18
    @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 '16 at 19:08
  • 6
    ^^^ the practice of which is killing SO, IMO. – Geek Stocks Jan 06 '17 at 05:34
  • **See also:** https://stackoverflow.com/questions/2432579/git-your-branch-is-ahead-by-x-commits – dreftymac Dec 12 '17 at 22:45
  • late to the party but this might simply mean that your branch is fine and it is ahead of the origin you cloned from and you might not want to update that if it was some template for your django app or something, right.. – Vitaliy Terziev Jul 30 '19 at 12:01

9 Answers9

942

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/master
Acumenus
  • 41,481
  • 14
  • 116
  • 107
iberbeu
  • 12,251
  • 5
  • 24
  • 47
  • 111
    git reset --hard origin/master is exactly what I was looking for. Thanks. – FluxEngine Apr 29 '13 at 21:23
  • 4
    @iberbeu you solved my day...git reset --hard origin/master is what I am looking for. +1ed – Ravi Jul 17 '13 at 14:30
  • 84
    Also fwiw `git diff master origin/master` (ie. `git diff local remote`) to see changes you'll be removing – Shanimal Jul 10 '14 at 03: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 '14 at 16:28
  • 2
    Last one got me what I needed there! – RyanG Nov 19 '15 at 20:19
  • I am kind of having the same issue. But the thing is I tried pushing local commits to the origin using `git push origin master` and after that when I run `git status` I am still getting the same output saying that `Your branch is ahead of 'remote-branch-name/master' by commits.` – Rajith Gun Hewage Feb 02 '16 at 08:37
  • It turned out that I had two target names pointing to the same remote url (origin and the one created from the IDE) and when I push to the origin the IDE says that the `IDE-created-target/master` is behind. – Rajith Gun Hewage Feb 02 '16 at 08:40
  • 1
    Anyone trying this answer should be aware that origin/master is not necessarily the branch you want to reset to.......... – Amalgovinus Aug 19 '16 at 19:04
  • git reset --hard origin/master did it for me! --hard HEAD~ wasn't bringing the project inline with the remote! – Emmanuel Buckshi Jan 19 '17 at 20:04
  • $ git push origin master remote: Forbidden fatal: unable to access 'https://rajkanase11@bitbucket.org/hrishiawari/electron.git/': The requested URL returned error: 403 .......getting above error while pushing the code – Raj Oct 14 '18 at 08:24
  • `git reset --hard origin/master` will delete your changes that you tried to push, but how to preserve them? – ACV Aug 30 '19 at 13:15
  • A BIG THANK YOU @iberbeu @Acumenus! This ```--hard git reset``` finally did what I was trying to do for hours now!! – Nayyar Mar 27 '20 at 11:31
  • Also _also_ fwiw `git log origin/master..master` is useful to see the commit logs that have not yet been pushed. – heyhey2k Feb 17 '21 at 17:52
45

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.

pmr
  • 54,366
  • 9
  • 104
  • 149
  • I used git pull --rebase, but it now says I am ahead by one commit – FluxEngine Apr 29 '13 at 21:12
  • 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 '13 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 '13 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 '13 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 :-( – Sander de Jong Nov 29 '16 at 10:26
  • I had committed changes to my master then deleted my branch and I wasn't sure how to get the changes to the remote since I deleted my branch. "git push" by itself defaults to local and pushed my changes to the remote master. – DanGoodrick Jul 18 '19 at 19:46
44

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.

Abhishek Goel
  • 15,517
  • 8
  • 81
  • 62
19

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

Had to do

git fetch

and that was it.

pragman
  • 1,331
  • 13
  • 19
  • 2
    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! – Martin Meeser Sep 11 '17 at 12:17
  • 1
    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 :) – Chris Rutledge Jun 01 '20 at 22:02
13

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...

Alex
  • 2,084
  • 1
  • 21
  • 20
  • 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. – philo vivero Feb 24 '20 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.

Tim Parenti
  • 501
  • 6
  • 26
Sylvain Defresne
  • 38,469
  • 11
  • 70
  • 82
7

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

Pratik Khadka
  • 583
  • 6
  • 15
5

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!!!

Olumide
  • 283
  • 4
  • 13
1
$ git fetch

  - remote: Enumerating objects: 3, done.
  - remote: Counting objects: 100% (3/3), done.
  - remote: Compressing objects: 100% (3/3), done.
  - remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0


$ git pull 

   - Already up to date!
   - Merge made by the 'recursive' strategy.

finally:

$ git push origin
sandes
  • 1,190
  • 11
  • 22
  • This doesn't answer the original question, and has no explanation. Also only works if you have push-permissions, which may be less likely in a "non-trivial" situation. – MarkHu Aug 25 '20 at 16:43