Home > OS >  Your branch is ahead of 'origin/branch' by 1 commit
Your branch is ahead of 'origin/branch' by 1 commit

Time:03-30

I know that this question has already been talked a lot in SO. But please bear with me.

My senior had created a branch called mav921, I was instructed to checkout that branch and work on it.

So what I did was, git checkout -b local-mav921 origin/mav921,

It worked.

As I finished my task being in local-mav921 branch, I wanted to push it, so I did

git add .
git commit -m "message"

but the commit said:

Your branch is ahead of 'origin/mav921' by 1 commit.
(use "git push" to publish your local commits)

Changes not staged for commit:
... all the files that I was working.

Basically, what happened is after running add & commit, it did not commit my work.

so, I tried to push as per error message: git push, and it says:

fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

git push origin HEAD:mav921

To push to the branch of the same name on the remote, use

git push origin HEAD

when running git branch -a:

    Mav921
    mav921
*   local-mav921
    master
    remotes/origin/mav921
    remotes/origin/master

You can notice that I have Mav921 (don't mind this because I created this from master).
. also, mav921 (This is because I checked out the one from remote without creating a new branch that bases to that branch). And finally, local-mav921 (is the result of running git checkout -b local-mav921 origin/mav921)

To be honest, I don't have any idea how to resolve this. It would be great if someone can enlighten me as to what is happening and how to resolve this.

CodePudding user response:

So what I did was, git checkout -b local-mav921 origin/mav921

So now your local branch has a different name from the remote branch that it tracks. That's unusual but neither illegal nor fatal. But it does mean that certain Git shortcut commands won't work.

Basically, what happened is after running add & commit, it did not commit my work.

Yes it did. It committed your work just fine. That is why you are ahead of the remote; you made a new commit.

I tried to push as per error message: git push, and it says: fatal: The upstream branch of your current branch does not match the name of your current branch

Indeed. As I said at the start, you've done an unusual thing. Your local and remote branch names don't match. This means that shortcuts like simple git push don't work. You therefore have to give the command in its full form. But that's no problem! It's easy. And the message tells exactly you how to do it:

git push origin HEAD:mav921

So do that.

  •  Tags:  
  • git
  • Related