FluxEngine
FluxEngine

Reputation: 13310

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

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?

Upvotes: 608

Views: 774608

Answers (13)

iberbeu
iberbeu

Reputation: 16235

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

Upvotes: 1278

Kay V
Kay V

Reputation: 4046

Before executing commands, look at the actual differences

The following command will change nothing in your local or your remote. It will just show you the differences:

git log -p {branch_name}..origin/{branch_name}

If I'm on the dev branch, for example, I would enter git log -p dev..origin/dev.

Why look first?

Seeing what's different on your remote may remind you why there is a difference, and it may affect your decision about how to resolve the situation.

Making them identical without checking could really complicate things more. Keep in mind, you may want

  • what's on your local branch to replace what's on the remote, or
  • what's on the remote branch to replace what's on your local, or even
  • to preserve both what's on your local and what's on the remote

Upvotes: 1

Jason Gaskin
Jason Gaskin

Reputation: 11

In my case none of the suggested answers worked. When I tried git reset --hard main/main I was taken back to some commits from months ago. I ended up deleting my local repo, cloning the remote repo and then adding any ignored files from the initially deleted repo for a clean start

Upvotes: 0

msb
msb

Reputation: 4438

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.

Upvotes: 2

Ankita Agarwal
Ankita Agarwal

Reputation: 159

git reset --hard origin/master

Use this command

Upvotes: 10

Alkum
Alkum

Reputation: 21

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

Upvotes: 1

Sudhir Vishwakarma
Sudhir Vishwakarma

Reputation: 805

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

Upvotes: 8

Pratik Khadka
Pratik Khadka

Reputation: 958

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

Upvotes: 25

Zioalex
Zioalex

Reputation: 4333

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

Upvotes: 18

pragman
pragman

Reputation: 1644

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

Had to do

git fetch

and that was it.

Upvotes: 29

Abhishek Goel
Abhishek Goel

Reputation: 19771

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.

Upvotes: 71

Sylvain Defresne
Sylvain Defresne

Reputation: 44593

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.

Upvotes: 11

pmr
pmr

Reputation: 59841

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.

Upvotes: 49

Related Questions