1

I have a question about creating branch:

First, I create a branch from origin/master

$ git checkout -b dev origin/master
$ git push origin dev

after modify file, then

$ git add .
$ git commit -m "add ii"
$ git status
On branch dev
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

$ git push origin dev
$ git status
On branch dev
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

I found origin/dev also has the commit log what is add ii .

Why does git prompt the message what is Your branch is ahead of 'origin/master' by 1 commit ?

gao
  • 23
  • 1
  • 3

4 Answers4

1

Running through what you've done so far:

Assuming you have a remote repo with a master branch: git checkout -b dev origin/master will create a local branch named dev, based on the origin/master, and tracking the origin/master branch. You can verify the branch that is being tracked (an upstream branch) by using git branch -vv it will give you some output that looks like:

dev      <commit> [origin/master] <message>
master   <commit> [origin/master] <message>

Note, that part in square brackets shows you the upstream branch, which is what git uses to decide if your local branch is in sync with your remote branch. This is likely where the confusion is coming from.

You then ran git push origin dev, this pushed your current local branch to a branch named dev on the remote server. If you verified this with git branch -r to view remote branches, you would likely see:

origin/dev
origin/master

You then created an additional commit in the dev branch, and ran git push origin dev again, this pushed the changes to the dev branch on the remote server.

When you run git status, git will compare your local branch dev to the remote branch it is tracking (origin/master), sinceorigin/master has not been altered, it is still 1 commit/change behind your current dev branch (and origin/dev), hence the message.

I suspect what you'll want to do is change your local dev branch to track the remote dev branch. That is covered in better detail by other answers, but in short, you can run git branch --set-upstream-to=origin/dev. Alternatively you can append --set-upstream to your push command: git push origin dev --set-upstream.

Chris
  • 8,268
  • 3
  • 33
  • 46
1

TL;DR: run git branch --set-upstream-to=origin/dev

You are seeing that message because you have the upstream of your dev branch set to origin/master. You successfully pushed from your local dev branch to origin/dev, but your local dev upstream is still set to origin/master. You can think of that message as:

Your branch is ahead of '<upstream-of-current-branch>' by 1 commit.

You probably want to set your upstream to origin/dev, like this:

git branch --set-upstream-to=origin/dev

Then when you run git status you should see this instead:

On branch dev  <----------------------------------- your local, current branch
Your branch is up-to-date with 'origin/dev'.  <---- upstream is up-to-date
nothing to commit, working tree clean  <----------- local branch is up-to-date

To sync master, I would recommend you go through Github or whatever git repo service is hosting your remote repo. Through that service, create a pull request and then you/the relevant person on your team can accept the pull request, which will bring master up-to-date.

Brendan Goggin
  • 2,061
  • 14
  • 14
0

Take into account that you have created the branch from master and you have published that branch:

git push origin dev

It seems you're still in dev branch and message just sais that your current status (in dev branch) is ahead of master by one commit. Without any more information, grom my point of view, that's correct because your commit hasn't been performed in origin/master but in "dev" branch, so your branch is ahead as the message sais.

[edit]

Change to master again and merge from "dev" branch to publish the changes in master:

git checkout master
git merge dev
Jorge
  • 1,136
  • 9
  • 15
0

Here you have a branch develop created from Master. So Master branch is parent and Develop is child.

So when you are committing in your branch that is develop. It compares the history with its parent that is Master. And it lets you know that your branch is ahead that is one commit is more that master in your branch.

Once you will merge your develop branch with master it will not show you this.. Cz now your parent branch contains all the commits.

Niki
  • 1,566
  • 1
  • 19
  • 36