简体   繁体   中英

Local branch ahead of master after git pull

I am working on a remote server that does not have my git credentials, and I want to pull from master. I have no local commits, I just want to update the local repository on this machine to match the remote repository (origin). Using this answer I managed to update the repository:

my_repo$ git pull https://my_user@github.com/my_repo
Password for 'https://my_user@github.com': 
<some updates....>

Indeed, after this action, the local repository was updated to include all the commits exactly as on the remote repository.

But, for some reason I cannot fathom, the local repository became ahead of master, and I can't find a way to fix it. How is that possible???

my_repo$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
    

my_repo$ git push https://my_user@github.com/my_repo
Password for 'https://my_user@github.com': 
Everything up-to-date

my_repo$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

my_repo$ git pull https://my_user@github.com/my_repo
Password for 'https://my_user@github.com': 
From https://github.com/my_repo
 * branch            HEAD       -> FETCH_HEAD
Already up to date.

Also, when I use git log I see this strange outcome, that corresponds to my branch being ahead of master, but does not correspond to "reality":

my_repo$ git log --pretty=oneline
09ee1f2 (HEAD -> master) Latest commit on master
b6433fb Another commit from master
31da031 Another commit from master
85b95ae (origin/master, origin/HEAD) Another commit from master which was the head before pulled

Anyone has any advice on how to fix this? And any thoughts on how this happened and how I can avoid this in the future?

Thanks!

Your origin/master is out of date.

Pulling from a URL is a one-off. It does not update origin/master.

origin is your name for a remote repository associated with a particular URL. origin/master is the last time you saw the master branch on origin. origin/master will only be updated with a git fetch or git pull from origin. This hasn't happened.

Instead, you've updated master by pulling from some different URL. It might be the same repository as origin, but Git does not know that. You can fix that by changing the origin url with git remote set-url origin https://my_user@github.com/my_repo . Then git fetch origin .

See Remote Branches in Pro Git.

Thanks to @Schwern's answer , I was able to understand the problem.

How I solved it:

  1. Revert "local" commits using this wonderful website:

     git reset --hard @{u}
  2. Make sure there are no existing credentials on this repository on this machine by removing credentials from gitconfig :

     sudo subl ~/.gitconfig // or any other text editing instead of subl
  3. Pull "regularly", that is, without specifying my credentials:

     my_repo$ git pull Username for 'https//github.com': my_user Password for 'https://my_user@github.com': <updates...>

And now everything is fine...

Thanks again @Schwern for explaining the problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM