1

I accidentally switched from my remote dev branch to local dev and now I cannot switch back.

When I try to:

 git fetch origin/master

I get

fatal: 'origin/master' does not appear to be a git repository. fatal: Could not read from remote repository.

I also tried:

git checkout origin/master

And I got:

error: pathspec 'origin' did not match any file(s) known to git. error: pathspec 'master' did not match any file(s) known to git.

I checked with git remote -v that the url for my origin was there. Also in config file the right url is indicated

remote.dev.url=https://'my_origin_url'.git
remote.dev.fetch=+refs/heads/*:refs/remotes/dev/*
remote.dev.pushurl=https://'my_origin_url.git
branch.dev.remote=dev
branch.dev.merge=refs/heads/dev
remote.origin.url=https://'my_origin_url'.git/
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

And I tried:

git reset --hard origin/master

The result was:

fatal: ambiguous argument 'origin/master': unknown revision or path not in the working tree.

How can I go back to my origin branch?

dark_matter88
  • 307
  • 3
  • 14
  • Those two separate `pathspec ...` errors indicate that you must have run `git checkout origin master`. Git can be terribly confusing here: when should you use `origin`, when `master`, when `origin/master`, and when `origin master`? The answers are all different! – torek Jan 28 '19 at 20:41
  • Yes, I probably did run `git checkout origin master` :( can you advise me what should I do next? – dark_matter88 Jan 29 '19 at 07:40
  • there was problem with connection to remote repo, I resolved it and successfully switched to origin branch, thank you for you advice – dark_matter88 Jan 29 '19 at 10:43

1 Answers1

1

When you fetch, you should only specify the remote, not the branch:

git fetch origin

To checkout master:

git checkout master

Since it looks like master is not already a local branch in your sandbox, it should get automatically recreated with origin/master as its upstream, which is what you want.

If it gets created with dev/master instead as its upstream, you will need to set its upstream branch explicitly.

Option 1: explicitly specify the upstream when you create the branch:

git checkout -b master -t origin/master

Option 2: update the upstream after the fact, if the branch exists with the wrong upstream:

git branch -u origin/master master    
joanis
  • 10,635
  • 14
  • 30
  • 40
  • I get `error: the requested upstream branch 'origin/master' does not exist` for the option 2. And for the option 1 I cannot do fetch either, I get `fatal: unable to access 'https://my_origin_url.git/': Failed connect to my_origin_url:443; Connection refused` – dark_matter88 Jan 29 '19 at 07:50
  • If `git fetch` fails with a connection refused, you'll need to provide your credentials, the same way you must have had to do when you did the initial `git clone`. You may need to update your ssh key or credentials within the git config. Have a look here for more information: https://stackoverflow.com/questions/35942754/how-to-save-username-and-password-in-git – joanis Jan 29 '19 at 14:20
  • `Connection refused` looks more like an issue with networking or with the configuration on the remote system. You should be able to see some more detail with `GIT_CURL_VERBOSE=1 git fetch origin`. – Jim Redmond Jan 29 '19 at 20:03