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

536,428

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

Had to do

git fetch

and that was it.

Solution 5

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

Share:
536,428
FluxEngine
Author by

FluxEngine

Updated on July 08, 2022

Comments

  • FluxEngine
    FluxEngine almost 2 years

    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?

  • FluxEngine
    FluxEngine about 11 years
    I used git pull --rebase, but it now says I am ahead by one commit
  • FluxEngine
    FluxEngine about 11 years
    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.
  • pmr
    pmr about 11 years
    @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.
  • FluxEngine
    FluxEngine about 11 years
    git reset --hard origin/master is exactly what I was looking for. Thanks.
  • FluxEngine
    FluxEngine about 11 years
    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.
  • Ravi
    Ravi almost 11 years
    @iberbeu you solved my day...git reset --hard origin/master is what I am looking for. +1ed
  • Shanimal
    Shanimal almost 10 years
    Also fwiw git diff master origin/master (ie. git diff local remote) to see changes you'll be removing
  • Will B.
    Will B. over 9 years
    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.
  • RyanG
    RyanG over 8 years
    Last one got me what I needed there!
  • Rajith Gun Hewage
    Rajith Gun Hewage over 8 years
    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 <some-number> commits.
  • Rajith Gun Hewage
    Rajith Gun Hewage over 8 years
    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.
  • Alkanshel
    Alkanshel almost 8 years
    Anyone trying this answer should be aware that origin/master is not necessarily the branch you want to reset to..........
  • Sander de Jong
    Sander de Jong over 7 years
    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 :-(
  • Emmanuel Buckshi
    Emmanuel Buckshi over 7 years
    git reset --hard origin/master did it for me! --hard HEAD~ wasn't bringing the project inline with the remote!
  • Martin Meeser
    Martin Meeser almost 7 years
    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!
  • Raj
    Raj over 5 years
    $ git push origin master remote: Forbidden fatal: unable to access '[email protected]/hrishiawari/electron.git': The requested URL returned error: 403 .......getting above error while pushing the code
  • DanGoodrick
    DanGoodrick almost 5 years
    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.
  • ACV
    ACV almost 5 years
    git reset --hard origin/master will delete your changes that you tried to push, but how to preserve them?
  • philo vivero
    philo vivero over 4 years
    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.
  • N a y y a r
    N a y y a r over 4 years
    A BIG THANK YOU 🙏🏼 @iberbeu @Acumenus! This --hard git reset finally did what I was trying to do for hours now!!
  • Chris Rutledge
    Chris Rutledge about 4 years
    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 :)
  • heyhey2k
    heyhey2k over 3 years
    Also also fwiw git log origin/master..master is useful to see the commit logs that have not yet been pushed.
  • sluge
    sluge almost 3 years
    I had the same issue 'git reset --hard origin/master' is helped me)
  • Leonardo Alves Machado
    Leonardo Alves Machado about 2 years
    duplicated from this previous answer