Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git branch is ahead of origin/master while repo is in sync

Tags:

git

github

Folks,

This is something in git that just does not make sense to me, here is what happens

  • I do a git status, I see that I am on branch master and nothing to commit and working directory is clean.
  • I then do git pull origin master I pull a bunch of code, no issues.
  • Now when I do git status I see a new line Your branch is ahead of origin/master by 1 commit

My local repo and remote repo are totally in sync, what does your branch is ahead of origin/master by 1 commit mean, this is very very confusing.

like image 301
user1781472 Avatar asked Mar 22 '13 12:03

user1781472


People also ask

How can a branch be ahead and behind Master?

A branch can be both ahead and behind at the same time if you have made changes to your branch, and someone else have made changes on the default branch that you have not merged into your branch yet.

Why is my local branch ahead of origin?

It means that you have some commits in your branch that weren't pushed to origin. To keep your local branch in sync with origin, you need to push your code to that frequently.

How do I sync my branch with GitHub master?

In GitHub Desktop, click Current Branch. Click Choose a branch to merge into BRANCH. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH. Note: If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button.


2 Answers

I have experienced exactly what you see. I can't find the proper explanation on git-scm.com but I believe it to be something like this:

  1. I clone a repo from origin, lets say origin has master and I have master now in my local repo
  2. now origin and your repo have a reference to the last commit. This reference is the master branch AND the reference is the same in your local repo and on origin
  3. Let's say someone (other then you) pushes new commits to origin master
  4. you do git pull origin master
  5. now you do git status and you will see that your branch is ahead to origin/master even tough you have no new commits in your local repo and nothing to push!! (in this example, you did not commit anything new locally since step 1 (the cloning))

==> I fix this by doing a git pull origin when I'm on master.

A git pull origin master will pull all new commits from a branch on origin to your local branch.

A git pull origin will also re-set your reference to master equal to the commit where master is referenced on origin!! (When this didn't happen, the git-bash will just think that you are alot of commits ahead! because in the commit-tree there are alot of commits made after the commit where your master-reference is!)

Does this make any sence to you? It does to me :)

Also please feel free to support my toughts a bit with hard evidence/documentation :)

like image 52
deblendewim Avatar answered Sep 20 '22 00:09

deblendewim


You have added a commit on your local machine, which has not yet been sent to the remote server.

If you are confident your modifications should be shared with the remote repository, sending the commit to the remote is done with the git push command :

git push orgin master:master
like image 21
LeGEC Avatar answered Sep 21 '22 00:09

LeGEC