2

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!

DalyaG
  • 2,979
  • 2
  • 16
  • 19
  • Are you pushing to the same place from which you are pulling? What is the output of `git remote -vv`? – William Pursell Jul 16 '20 at 07:21
  • Yes @WilliamPursell, it's the same repo. `git remote -vv` gives the output `origin https://github.com/my_repo.git (fetch) \n origin https://github.com/my_repo.git (push)`, which is exactly the same output as on my personal computer – DalyaG Jul 16 '20 at 07:25
  • Let's go through the trivial: Your repo was already on 85b95ae when you started working on it, you created three local commits, you pulled (there is nothing to pull then), your master is ahead. Can this be the case? I'd say yes due to "Already up to date." in the pull message. – Daemon Painter Jul 16 '20 at 07:33
  • @DaemonPainter actually I made NO local commits. That's the mystery. The 3 new commits that are now "ahead of master" are commits that I pulled *from* origin master. I made those commits on my personal computer... :| – DalyaG Jul 16 '20 at 07:39
  • Maybe [edit]() your question to say this with more emphasis. How many machines are involved in this? I'd say three now: remote, local and your personal. Moreover, you state that 85b95ae was the head before pulling, further confirmation that your local was there. But, just in case, go in the remote and check which is the HEAD commit. Print the SHA please – Daemon Painter Jul 16 '20 at 07:43
  • @DaemonPainter I have updated my question to emphasize I have no local commits and just want to pull from origin. This case is solved, see Schwern's answer. – DalyaG Jul 16 '20 at 08:39

2 Answers2

4

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.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Thanks for your answer! The thing is, this is a remote machine, I am not the only one working on it, and if I change the remote address, then the next person will have to change it too, right? (Is there a way to work collaboratively on the same computer?) – DalyaG Jul 16 '20 at 07:42
  • @DalyaG I'm not sure what you mean. Are you sharing an account or the cloned repository with other users? – Schwern Jul 16 '20 at 07:56
  • @DalyaG If you are sshing into a remote machine and using a repository there and you do not want to copy your Github ssh key, you can use [SSH Agent Forwarding](https://docs.github.com/en/developers/overview/using-ssh-agent-forwarding) to use your local ssh credentials on a remote machine. – Schwern Jul 16 '20 at 07:57
  • it's a remote machine that both I and another colleague are working on, and we both want to able to pull and push and do stuff, with our own separate credentials. – DalyaG Jul 16 '20 at 08:14
1

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!

DalyaG
  • 2,979
  • 2
  • 16
  • 19